如何使RecyclerView更平滑?

时间:2018-11-29 16:56:48

标签: java android listview android-recyclerview adapter

我正在编写体育应用程序。我必须在此应用程序的列表中显示所有游戏。 以前我使用过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 :(得分:0)

有些问题引人注目

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上进行设置。

用{p>重复league_enter.setText( "\uf141" )

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