我的列表视图滞后可以有人帮我解决这个问题吗?

时间:2017-07-07 09:55:51

标签: android xml android-layout android-fragments view

我发布我的适配器类和布局。任何人都可以建议我如何摆脱这个滞后的问题。 我试过看视图持有人,但它没有工作。 所以任何其他解决方案都可以快速告诉我。 我的布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp">
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp">
                <View
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"

                    android:background="@drawable/circle2"/>
                <com.hitesh.custom.Custom_font
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:textColor="#fff"
                    android:textSize="13sp"

                    android:layout_gravity="center"
                    android:text="&#xf10d;"/>

            </FrameLayout>
            <com.hitesh.custom.Custom_Roboto_Regular
                android:id="@+id/tv1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textSize="25sp" />

        </LinearLayout>

        <com.hitesh.custom.Custom_Roboto_Light
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:lineSpacingExtra="1dp"
            android:textSize="18sp" />

     </LinearLayout>

   </android.support.v7.widget.CardView>

   </LinearLayout>

和适配器

public class Rules_d_adapter  extends BaseAdapter{

private Context context;
List<String> question =new ArrayList<String>();
List<String> answer =new ArrayList<String>();
public Rules_d_adapter(Context con,List<String> Lines,List<String> Lines2)
{question=Lines;
    answer=Lines2;
    context = con;
}

@Override
public int getCount() {
    return question.size();
}

@Override
public Object getItem(int i) {
    return question.size();
}

@Override
public long getItemId(int i) {
    return question.size();
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater) 
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.rules_d_adapter, viewGroup, false);
    Custom_Roboto_Light tv2 = 
    (Custom_Roboto_Light)view.findViewById(R.id.tv2) ;
    Custom_Roboto_Regular tv = (Custom_Roboto_Regular) 
    view.findViewById(R.id.tv1) ;
    tv.setText(question.get(i));
    tv2.setText(answer.get(i));

    return view;
    }
    }

1 个答案:

答案 0 :(得分:0)

  

如果您仍然对ListView感到满意,那么您可以从下面了解一下   代码

这里我使用Cursor但您可以使用ArrayList代替Cursor

public class Custom_Adapter_Products extends BaseAdapter {
    Context context;
    Cursor mCursor; 
    static int[] intArr;
    DatabaseAdapter db;
    public ImageLoader imageLoader;

    public Custom_Adapter_Products(Context context, Cursor mCursor){
        this.context = context;
        this.mCursor = mCursor;
        this.intArr = new int[this.mCursor.getCount()];
        imageLoader=new ImageLoader(context);
    }

    @Override
    public int getCount() {     
        return mCursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {       
        try{            
                ViewHolder holder;
                this.mCursor.moveToPosition(position);
                LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (convertView == null)
                {
                    convertView = vi.inflate(R.layout.lv_row_for_products, null);
                    holder = createViewHolder(convertView);
                    convertView.setTag(holder);
                    holder.btnMinus.setTag(holder);
                    holder.btnAdd.setTag(holder);
                } 
                else
                {
                    holder = (ViewHolder) convertView.getTag();                
                }           

                holder.btnMinus.setOnClickListener(new OnClickListener() {              
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try{
                        ViewHolder holder = (ViewHolder) v.getTag();
                        // Do whatever you want
                    }catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            });



        }catch (Exception e){
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }   


    private static class ViewHolder
    {
        public TextView tvProductName;
        public TextView tvPrice;
        public TextView tvMRP;
        public TextView tvMOQ;
        public TextView tvPercent;
        public TextView tvQuantity;       
        public ImageView imgProduct;
        public ProgressBar pBar;
        public Button btnMinus;
        public Button btnAdd;       
    }

    private ViewHolder createViewHolder(View v) {
        ViewHolder holder = new ViewHolder();
        try{
            holder.tvProductName = (TextView) v.findViewById(R.id.tvProductName);
            holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
            holder.tvMRP = (TextView) v.findViewById(R.id.tvMRP);
            holder.tvMOQ = (TextView) v.findViewById(R.id.tvMOQ);
            holder.tvQuantity = (TextView) v.findViewById(R.id.tvQuantity);
            holder.imgProduct = (ImageView) v.findViewById(R.id.imgProduct);
            holder.pBar = (ProgressBar) v.findViewById(R.id.img_pb);
            holder.btnMinus = (Button) v.findViewById(R.id.btnMinus);
            holder.btnAdd = (Button) v.findViewById(R.id.btnAdd);
            holder.tvPercent = (TextView) v.findViewById(R.id.tvPercent);
        }catch (Exception e) {
        // TODO: handle exception
            e.printStackTrace();
    }
        return holder;
    }
}