gpt4 book ai didi

java - 将 ViewHolder 与 ListView 结合使用

转载 作者:行者123 更新时间:2023-12-01 11:38:11 29 4
gpt4 key购买 nike

我正在使用 ListView 来列出文本和图像,但我想使用 ViewHolder 使滚动更加平滑我已经尝试过,但不太清楚我必须如何修改代码

我尝试的一些图像没有显示

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

@Override
public Object getItem(int position) {
return text1[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater infla=getActivity().getLayoutInflater();

View v = infla.inflate(R.layout.list_view_layout, null);

TextView tv1 = (TextView) v.findViewById(R.id.textView1);
ImageView iv1 = (ImageView) v.findViewById(R.id.imageView1);
TextView tv2 = (TextView) v.findViewById(R.id.textView2);
ImageView iv2 = (ImageView) v.findViewById(R.id.imageView2);
TextView tv3 = (TextView) v.findViewById(R.id.textView3);
ImageView iv3 = (ImageView) v.findViewById(R.id.imageView3);
TextView tv4 = (TextView) v.findViewById(R.id.textView4);
ImageView iv4 = (ImageView) v.findViewById(R.id.imageView4);
TextView tv5 = (TextView) v.findViewById(R.id.textView5);
ImageView iv5 = (ImageView) v.findViewById(R.id.imageView5);
TextView tv6 = (TextView) v.findViewById(R.id.textView6);
ImageView iv6 = (ImageView) v.findViewById(R.id.imageView6);
TextView tv7 = (TextView) v.findViewById(R.id.textView7);
ImageView iv7 = (ImageView) v.findViewById(R.id.imageView7);

tv1.setText(text1[position]);
iv1.setImageResource(text2[position]);
tv2.setText(text3[position]);
iv2.setImageResource(text4[position]);
tv3.setText(text5[position]);
iv3.setImageResource(text6[position]);
tv4.setText(text7[position]);
iv4.setImageResource(text8[position]);
tv5.setText(text9[position]);
iv5.setImageResource(text10[position]);
tv6.setText(text11[position]);
iv6.setImageResource(text12[position]);
tv7.setText(text13[position]);
iv7.setImageResource(text14[position]);

if(text2[position]==R.drawable.ic_star){
iv1.setVisibility(View.GONE);
}if(text3[position].matches("")) {
tv2.setVisibility(View.GONE);
}if(text4[position]==R.drawable.ic_star){
iv2.setVisibility(View.GONE);
}if(text5[position].matches("")) {
tv3.setVisibility(View.GONE);
}if(text6[position]==R.drawable.ic_star){
iv3.setVisibility(View.GONE);
}if(text7[position].matches("")){
tv4.setVisibility(View.GONE);
}if(text8[position]==R.drawable.ic_star){
iv4.setVisibility(View.GONE);
}if(text9[position].matches("")){
tv5.setVisibility(View.GONE);
}if(text10[position]==R.drawable.ic_star){
iv5.setVisibility(View.GONE);
}if(text11[position].matches("")){
tv6.setVisibility(View.GONE);
}if(text12[position]==R.drawable.ic_star){
iv6.setVisibility(View.GONE);
}if(text13[position].matches("")){
tv7.setVisibility(View.GONE);
}if(text14[position]==R.drawable.ic_star){
iv7.setVisibility(View.GONE);
}

return v;
}

最佳答案

技巧:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if( convertView == null ){
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_layout, null);
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.textView);
holder.iv=(ImageView) convertView.findViewById(R.id.imageView);
...
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}

holder.tv.setText(text1[position]);
holder.iv.setImageResource(text2[position]);
...
//handle with your widgets cached in ViewHolder
return convertView;

}

并且ViewHolder在这里:

public class ViewHolder{
TextView tv;
ImageView iv;
}

关于java - 将 ViewHolder 与 ListView 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29791592/

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