gpt4 book ai didi

android - ListView的回收是如何工作的

转载 作者:行者123 更新时间:2023-11-29 21:02:22 25 4
gpt4 key购买 nike

我的项目中有一个 listView,我想在单击 listItem 时更改背景颜色。但是当我点击一行时,它改变了很多行。

这是我实现这个功能的代码:

 public View getView(int pos, View view, ViewGroup parent) {
mActivity = (Activity) this.getContext();
ListViewHolder holder;


if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.portfolio_row, null);

LinearLayout textViewWrap = (LinearLayout) view.findViewById(R.id.portfolio_text_wrap);
TextView text = (TextView) view.findViewById(R.id.portfolio_symbol);
holder = new ListViewHolder(textViewWrap, text);

} else
holder = (ListViewHolder) view.getTag();





PortfolioItem portfolioItem = getItem(pos);
}

但是当我点击一行时,它会改变很多行。

我得到了 pos 的 system.out.println。我应该得到它应该是 0 1 2 3 4 5 6 7 8 9(# 代表位置)。我现在得到的是这样一个循环:0 1 2 3 0 1 2 3 0 1

所以如果我点击一个,它会改变所有相同的位置。但是如果我不点击,直接拉下listView,位置是正确的。比如0----9。

这是我的项目点击代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
list = (ListView) root.findViewById(R.id.~~~);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {

toggle(view, position);
}
});
}

我想要的是在 listView 中获得它的绝对位置。请帮我解决这个问题。谢谢!

更新:谢谢大家的热心回答。但我认为你可能误解了我的问题。

  1. getView 更改位置。这意味着当你想改变第9行时,它不能改变,因为我只有0 1 2 3

  2. getView() 和 onCreateView() 属于不同的类。 System.out.println("pos"+ pos);在 getView 中无法获得正确的位置。

    正确的方法应该是 1 到最后一项。但是当我点击它时,数字将是一个循环。循环数是屏幕可以显示多少行。

就是这个链接How ListView's recycling mechanism works

而且我只想单击以获取一个正确的位置。非常感谢。

这里是 layout.xml:

 <LinearLayout a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="fill_parent">

<LinearLayout a:orientation="horizontal" a:layout_width="fill_parent" a:layout_height="wrap_content"
a:background="@color/onSelected">
<TextView a:text="@string/1" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:layout_margin="10dp" a:textStyle="bold" a:id="@+id/0"/>
<TextView a:text="@string/2" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
<TextView a:text="@string/3" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
<TextView a:text="@string/4" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
<TextView a:text="@string/5" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
<TextView a:text="@string/6" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
<TextView a:text="@string/7" a:layout_weight="1" a:layout_width="fill_parent"
a:layout_height="fill_parent" a:gravity="right" a:layout_margin="10dp" a:textStyle="bold"/>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:id="@+id/portfolios">
<ListView a:layout_width="fill_parent" a:layout_height="fill_parent" a:id="@+id/8" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

更新2:这是 listViewHolder:

public class ListViewHolder {
private LinearLayout textViewWrap;
private TextView textView;


public ListViewHolder(LinearLayout textViewWrap, TextView textView) {
super();
this.textViewWrap = textViewWrap;
this.textView = textView;
}

public TextView getTextView() {
return textView;
}

public void setTextView(TextView textView) {
this.textView = textView;
}

public LinearLayout getTextViewWrap() {
return textViewWrap;
}

public void setTextViewWrap(LinearLayout textViewWrap) {
this.textViewWrap = textViewWrap;
}
}

这里设置 fragment

class fragment{

list = (ListView) root.findViewById(R.id.123);
list.setAdapter(adapter);

}

在 fragment 类中我有 listItemClickListener,在适配器中我有 getView 函数

最佳答案

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {
adapter.changeVisable(view, position);


public void changeVisable(View view, int position) {
if (mLastView != null && mLastPosition != position) {
ListViewHolder holder = (ListViewHolder) mLastView.getTag();
switch (holder.getTxt().getVisibility()) {
case View.VISIBLE:
holder.getTxt().setVisibility(View.GONE);
mLastVisibility = View.GONE;
break;
default:
break;
}
}
mLastPosition = position;
mLastView = view;
ListViewHolder holder = (ListViewHolder) view.getTag();
switch (holder.getTxt().getVisibility()) {
case View.GONE:
holder.getTxt().setVisibility(View.VISIBLE);
mLastVisibility = View.VISIBLE;
break;
case View.VISIBLE:
holder.getTxt().setVisibility(View.GONE);
mLastVisibility = View.GONE;
break;
}
}

works perfect

关于android - ListView的回收是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25652914/

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