Как я могу сделать RecyclerView более плавным? - PullRequest
0 голосов
/ 29 ноября 2018

Я пишу спортивное приложение. Мне нужно показать все игры в списке в этом приложении.Ранее я использовал ListView, но теперь я использую RecyclerView, и его скорость лучше, но он все еще не совсем плавный и хорошо.

Как можно сделать это лучше и плавнее?

public class RecyclerAdapterMatch extends RecyclerView.Adapter<RecyclerAdapterMatch.CustomViewHolder> {

private ArrayList<String> ls = new ArrayList<>();

private LayoutInflater mInflater;

Context con;
boolean page_info_know;
String tv_stat;

String[][] weeks = {{"اول", "دوم", "سوم", "چهارم", "پنجم", "ششم", "هفتم", "هشتم", "نهم", "دهم", "یازدهم", "دوازدهم", "سیزدهم", "چهاردهم", "پانزدهم", "شانزدهم", "هفدهم", "هجدهم", "نوزدهم", "بیستم", "بیست و یکم", "بیست و دوم", "بیست و سوم", "بیست و چهارم", "بیست و پنجم", "بیست و ششم", "بیست و هفتم", "بیست و هشتم", "بیست و نهم", "سی‌ام", "سی و یکم", "سی و دوم", "سی‌ و سوم", "سی و چهارم", "سی و پنجم", "سی و ششم", "سی و هفتم", "سی و هشتم", "سی و نهم", "چهلم"},
        {"رده‌بندی", "فینال", "نیمه‌نهایی", "یک چهارم", "یک هشتم", "یک شانزدهم نهایی", "۱/۳۲ نهایی", "۱/۶۴ نهایی"},
        {"یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه", "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده", "بیست"}};


public RecyclerAdapterMatch(Context con) {//}, ArrayList<String> ls) {
    this.con = con;
    //this.ls = ls;
}

public void addItem(Object object) {
    ls.add( object.toString() );
}

@Override
public int getItemViewType(int position) {
    if (ls.get( position ).contains( "LeagueId" ))
        return 0;
    else return 1;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i/*in position nist*/) {

    CustomViewHolder leagueHolder = null;
    View v = null;


    try {

        if (i == 0) {
            v = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.ly_row_league, viewGroup, false );

        } else {

            v = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.row_result, viewGroup, false );

        }
    } catch (Exception e) {

    }

    return new CustomViewHolder( v );

}

public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {

    try {
        JSONObject js=new JSONObject( ls.get( position ) );

        if (js.has( "LeagueId" )) {

            holder.league_name.setTypeface( AppConfig.getFont( con ) );
            holder.league_enter.setTypeface( AppConfig.getFontMaterial( con ) );
            holder.league_enter.setText( "\uf141" );

            switch (js.getInt( "Type" )) {
                case 0:
                    holder.league_name.setText( js.getString( "LeagueName" ) );
                    break;
                case 1:
                    holder.league_name.setText( js.getString( "LeagueName" ) + " | هفته " + weeks[js.getInt( "Type" ) - 1][js.getInt( "Week" ) - 1] );
                    break;
                case 2:
                    holder.league_name.setText( js.getString( "LeagueName" ) + " | مرحله " + weeks[js.getInt( "Type" ) - 1][js.getInt( "Week" ) - 1] );
                    break;
                default:
                    holder.league_name.setText( js.getString( "LeagueName" ) + " | گروه " + weeks[js.getInt( "Type" ) - 1][js.getInt( "Week" ) - 1] );
                    break;
            }


            int League_Type = js.getInt( "Type" );
            if (League_Type == 0) {
                holder.rel_league.setClickable( false );
            }

            holder.line_league.setBackgroundColor( Color.parseColor( "#" + js.getString( "Color" ) ) );

            page_info_know = js.getBoolean( "PageInfo" );

        } else {

            holder.txt_time.setTypeface( AppConfig.getFontLightNumFa( con ) );
            holder.txt_homeName.setTypeface( AppConfig.getFontBold( con ) );
            holder.txt_guestName.setTypeface( AppConfig.getFontBold( con ) );
            holder.txt_homeName.setText( js.getString( "HostName" ) );
            holder.txt_guestName.setText( js.getString( "GuestName" ) );

            holder.txt_RT.setTextSize( 12 );
            holder.txt_RT.setTypeface( AppConfig.getFont( con ) );

            holder.txt_date.setTypeface( AppConfig.getFont( con ) );

            ////////
            if (js.getInt( "Status" ) == 1) {

                AlphaAnimation alpha = new AlphaAnimation( 0, 1 );
                alpha.setDuration( 500 );
                alpha.setRepeatCount( Animation.INFINITE );
                alpha.setRepeatMode( Animation.REVERSE );
                holder.txt_live.setVisibility( View.VISIBLE );
                holder.txt_live.setAnimation( alpha );
                holder.txt_live.setTypeface( AppConfig.getFont( con ) );
                holder.img_live.setVisibility( View.VISIBLE );
                holder.img_live.setAnimation( alpha );

                holder.txt_time.setText( String.format( new Locale( "fa", "IR" ), "%d", js.getInt( "GuestGoal" ) ) + " - " + String.format( new Locale( "fa", "IR" ), "%d", js.getInt( "HostGoal" ) ) );

            } else if (js.getInt( "Status" ) == 2) {
                holder.txt_time.setText( String.format( new Locale( "fa", "IR" ), "%d", js.getInt( "GuestGoal" ) ) + " - " + String.format( new Locale( "fa", "IR" ), "%d", js.getInt( "HostGoal" ) ) );

            } else if (js.getInt( "Status" ) == 0) {

                StringBuilder time = new StringBuilder( js.getString( "Time" ).replace( "0", "۰" ).replace( "1", "۱" ).replace( "2", "۲" ).replace( "3", "۳" ).replace( "4", "۴" ).replace( "5", "۵" ).replace( "6", "۶" ).replace( "7", "۷" ).replace( "8", "۸" ).replace( "9", "۹" ) );
                time.insert( 2, ":" );
                holder.txt_time.setText( time.toString() );

            } else if (js.getInt( "Status" ) == 3) {
                holder.rel_tv_status.setVisibility( View.VISIBLE );
                holder.txt_RT.setText( "لغو شده" );
            }

            tv_stat = js.getString( "RT" );

            if (page_info_know == false) {

                if (tv_stat == null || tv_stat.trim().length() == 0 || tv_stat.contains( "null" )) {
                    holder.rel_tv_status.setVisibility( View.GONE );
                } else {
                    holder.rel_tv_status.setVisibility( View.VISIBLE );
                    holder.txt_RT.setText( tv_stat );
                }
            } else {
                holder.rel_tv_status.setVisibility( View.VISIBLE );
                holder.txt_RT.setText( tv_stat );
                StringBuilder time = new StringBuilder( String.format( new Locale( "fa", "IR" ), "%d", js.getInt( "Date" ) ) );
                time.insert( 4, "/" );
                time.insert( 7, "/" );
                holder.txt_date.setText( time.toString() );
            }

            if (js.getString( "IsVideo" ).contains( "true" ) /*|| js.getString( "IsVideo" ).equals("null")*/) {
                holder.text_info.setVisibility( View.VISIBLE );
                holder.text_info.setTypeface( AppConfig.getFontMaterial( con ) );
                holder.text_info.setText( "\uf40d" );
            } else {
                holder.text_info.setVisibility( View.GONE );
            }

        }

    } catch (Exception e) {

    }

}


@Override
public int getItemCount() {
    if (ls != null) {
        return ls.size();
    }
    return 0;
}

static class CustomViewHolder extends RecyclerView.ViewHolder {


    protected TextView txt_homeName, txt_guestName, txt_time, txt_RT, txt_live, league_name, league_enter, txt_date, text_info;
    protected RelativeLayout rel_tv_status, rel_league;
    protected ImageView img_live, img_league;
    protected LinearLayout line_league;

    public CustomViewHolder(View view) {
        super( view );
        txt_homeName = view.findViewById( R.id.textView10 );
        txt_guestName = view.findViewById( R.id.textView9 );
        txt_time = view.findViewById( R.id.textView3 );
        rel_tv_status = view.findViewById( R.id.tv_status_rel );

        txt_RT = view.findViewById( R.id.textView170 );
        txt_live = view.findViewById( R.id.textView74 );
        img_live = view.findViewById( R.id.imageView65 );
        league_name = view.findViewById( R.id.textView160 );
        league_enter = view.findViewById( R.id.textView161 );
        img_league = view.findViewById( R.id.imageView69 );
        line_league = view.findViewById( R.id.league_line_color );
        rel_league = view.findViewById( R.id.line_league_all );
        txt_date = view.findViewById( R.id.textView174 );
        text_info = view.findViewById( R.id.textView176 );

    }
}


}

1 Ответ

0 голосов
/ 30 ноября 2018

Есть некоторые проблемы, которые бросаются в глаза

1.Не создавайте новый объект внутри на onBindViewHolder

Например, переместите эту часть ниже на RecyclerAdapterMatch(Context con)

AlphaAnimation alpha = new AlphaAnimation( 0, 1 );
alpha.setDuration( 500 );
alpha.setRepeatCount( Animation.INFINITE );
alpha.setRepeatMode( Animation.REVERSE );

2.Не создавайте шрифт каждый раз, когда вы устанавливаете шрифт, скорее, создавайте шрифт при инициализации активности или адаптера, делайте его статичным и используйте его при каждом другом вызове.Например, замените holder.league_name.setTypeface( AppConfig.getFont( con )); на holder.league_name.setTypeface(staticFontName);

3.Не устанавливайте текст одним и тем же текстом программно, если он всегда применяется к просмотру текста, лучше установите его на xml.

Repalce league_enter.setText( "\uf141" ) с

<TextView 
     android:id="+@textView161"
     android:text="\uf141"
...