gpt4 book ai didi

android - 优化 listview 性能 Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:08 27 4
gpt4 key购买 nike

我有一个 listView(垂直),每个列表项都有一个水平 ListView (水平)。

但问题是当我滚动行中的水平 ScrollView 时,垂直列表也在调用 getView()...

因此,性能受到了巨大的影响……那么,谁能告诉我更好的解决方案..

    public class GridViewAdapter extends BaseAdapter {

List<List<Hotel>> gridDatasource;
Context mContext;

public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}

public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}

@Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}

@Override
public Object getItem(int position) {
return gridDatasource.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);

holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}

}

static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}

public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;

public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}

@Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}

@Override
public Object getItem(int position) {
return mHotels.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}

最佳答案

亲爱的,我建议试试我的代码

public View getView(final int position, View convertView, ViewGroup parent) 
{
View v = convertView;
ViewHolder holder;

if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}

使用 Holder 类

关于android - 优化 listview 性能 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com