从上到下滚动时,Android列表视图项目是否在洗牌? [英] Android listview items shuffling when scrolling from top to bottom?

查看:94
本文介绍了从上到下滚动时,Android列表视图项目是否在洗牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义适配器填充了我的列表视图和文本视图以及按钮控件。但是当我滚动列表视图时,列表视图中的数据正在洗牌。



  public   class  CustomAdapter  extends  BaseAdapter 
{
LayoutInflater mInlfater;
ArrayList< hashmap>< string,string>>列表;
字符串 rno = ,name = ;
public CustomAdapter(Context context,ArrayList< hashmap>< string,string>> list)
{
mInlfater = LayoutInflater 。从(上下文);
this .list = list;
}
@覆盖
public int getCount()
{
return list.size();

}

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

@覆盖
public long getItemId( int position)
{
返回位置;
}

@覆盖
public 查看getView ( final int position,View convertView,ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = mInlfater.inflate(R.layout.attendancelistview,null);
holder = new ViewHolder();
holder.b1 =(Button)convertView.findViewById(R.id.row3);
holder.tv1 =(TextView)convertView.findViewById(R.id.row1);
holder.tv2 =(TextView)convertView.findViewById(R.id.row2);
convertView.setTag(holder);
}
else
{
holder =(ViewHolder)convertView.getTag();
}
HashMap< string,string> map = list.get(position);
holder.tv1.setText(map.get( Rno));
holder.tv2.setText(map.get( StudentName));
holder.b1.setOnClickListener( new OnClickListener()
{

@Override
public void onClick(查看v)
{
if (holder.b1.getText()。equals( Present))
{
holder.b1.setText( Absent);
list.get(position).put( 出勤 缺席);
holder.b1.setTextColor( Color.RED);

}
其他 如果(持有人) .b1.getText()。equals( 缺席))
{
holder.b1.setText( Present);
list.get(position).put( 出勤率 Present);
holder.b1.setTextColor(Color.GREEN);

}
else
{
holder.b1.setText( Present);
list.get(position).put( 出勤率 Present);
holder.b1.setTextColor(Color.GREEN);
}
字符串 rno1 = 字符串 .valueOf(holder.tv1.getText( ));
name = String.valueOf(holder.tv2.getText());
}

});
return convertView;
}
静态 class ViewHolder
{
Button B1;
TextView tv1,tv2,tv3;
}

}

解决方案

我建​​议您使用Recycler视图而不是ListView

Recycler视图附带更多装饰和方法,绝对可以帮到你。

查看官方文档页面

RecyclerView  |  Android开发者 [ ^ ]



滚动时回收站视图存在已知问题

滚动时将项目随机播放。当我初学者时,我花了几个小时来找到它的问题。

有多种解决方案你可以做if else条件但是不建议你需要覆盖Recyclerview两种方法

getItemId()和getITemViewType这将解决你的问题问题

解决方案参考:在滚动,Recyclerview改组或更改列表项上的值解决方案精神 [ ^ ]

I had populate my list view with text views and button control using custom adapter.But when i am scrolling list view the data inside the list view is shuffling.

public class CustomAdapter extends BaseAdapter
{
	LayoutInflater mInlfater;
	ArrayList<hashmap><string,string>> list;
	String rno="",name="";
	public CustomAdapter(Context context,ArrayList<hashmap><string,string>> list) 
	{
		mInlfater = LayoutInflater.from(context);
		this.list =list;
	}
	@Override
	public int getCount() 
	{
		return list.size();

	}

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

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

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		final ViewHolder holder;
		if(convertView ==null)
		{
			convertView = mInlfater.inflate(R.layout.attendancelistview,null);
			holder = new ViewHolder();
			holder.b1 = (Button)convertView.findViewById(R.id.row3);
			holder.tv1 = (TextView)convertView.findViewById(R.id.row1);
			holder.tv2 = (TextView)convertView.findViewById(R.id.row2);
		   convertView.setTag(holder);
		}
		else
		{
			holder =(ViewHolder) convertView.getTag();
		}
		HashMap<string,string> map = list.get(position);
		holder.tv1.setText(map.get("Rno"));
		holder.tv2.setText(map.get("StudentName"));
		holder.b1.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if(holder.b1.getText().equals("Present"))
				{
					holder.b1.setText("Absent");
					list.get(position).put("Attendance", "Absent");
					holder.b1.setTextColor(Color.RED);

				}
				else if(holder.b1.getText().equals("Absent"))
				{
					holder.b1.setText("Present");
					list.get(position).put("Attendance", "Present");
					holder.b1.setTextColor(Color.GREEN);

				}
				else
				{
					holder.b1.setText("Present");
					list.get(position).put("Attendance", "Present");
					holder.b1.setTextColor(Color.GREEN);
				}
				String rno1=  String.valueOf(holder.tv1.getText());
				name=String.valueOf(holder.tv2.getText());
			}

		});
		return convertView;
	}
	static class ViewHolder
	{
		Button b1;
		TextView tv1,tv2,tv3;
	}

}

解决方案

I would suggest you to use recycler view instead of ListView
Recycler view comes with more decoration and methods which will definitely help you.
Have a look to offical docs page
RecyclerView  |  Android Developers[^]

And there is a know issue with recycler view when scrolling
It shuffles the item when scroll. When i was beginner it took me several hours to find its issue.
There are multiple solutions to it You can do if else conditions but it is not recommended you need to override Recyclerview two methods
getItemId() and getITemViewType And this will solve your problem
Solution reference: On Scrolling, Recyclerview shuffles or change values on list item | Solution Spirit[^]


这篇关于从上到下滚动时,Android列表视图项目是否在洗牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆