与人交互 element 在 Android ListView 影响多

发布时间: 2017/3/19 13:24:21
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个列表 view 的搜索从各种 web 页面中提取的项目像这样︰

Visual Aid

那里是水平滚动 view 在标题上,用户可以完全看到的标题。然而,当我滚动的项目之一,约 6 下来从我相互作用与该项目的另一个项目也向右滚动。如何防止这种情况的发生?

更新︰ 这里是我的 CustomAdapter 代码︰

public class CustomAdapter extends ArrayAdapter<Item> implements View.OnClickListener{

private ArrayList<Item> dataSet;
Context mContext;

// View lookup cache
private static class ViewHolder {
    TextView itemTitle;
    TextView itemStore;
    TextView itemPrice;
    ImageView storeLogo;
}

public CustomAdapter(ArrayList<Item> data, Context context) {
    super(context, R.layout.row_item, data);
    this.dataSet = data;
    this.mContext=context;

}

@Override
public void onClick(View v) {

    int position=(Integer) v.getTag();
    Object object= getItem(position);
    Item item=(Item)object;

    switch (v.getId())
    {
        case R.id.store:
            Snackbar.make(v, "This item is being sold at " + item.getStore() + "." , Snackbar.LENGTH_LONG)
                    .setAction("No action", null).show();
            break;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Item item = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_item, parent, false);
        viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
        //viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
        viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
        viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);

        result = convertView;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result = convertView;
    }

    viewHolder.itemTitle.setText(item.getTitle());
    viewHolder.itemPrice.setText("$" + item.getPrice());
    viewHolder.storeLogo.setOnClickListener(this);
    viewHolder.storeLogo.setTag(position);

    // Return the completed view to render on screen
    return convertView;

}

}

这里是我的 fragment_search.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.painlessshopping.mohamed.findit.Search$PlaceholderFragment">



<Button
    android:text="Next Page"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonUp"
    android:onClick="buttonClicked"
    android:elevation="0dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="49dp" />

<Button
    android:text="Previous Page"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonDown"
    android:onClick="buttonClicked"
    android:elevation="0dp"
    android:layout_marginStart="62dp"
    android:layout_alignParentBottom="true"
    android:layout_toEndOf="@+id/buttonUp" />

<EditText
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_width="250dp"
    android:hint="What can I help you find?"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<ListView
    android:layout_width="match_parent"
    android:id ="@+id/listView"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true"
    android:layout_height="375dp" />

<Button
    android:text="Search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/searchBtn"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/editText" />

这里是我的 row_item.xml (自定义 adapter )

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:maxLines="1"
        android:text="Sample Text for Android Horizontal Scroll View Goes Here"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="@color/dgrey"/>

</HorizontalScrollView>


<ImageView
    android:id="@+id/store"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_favorite_border_black_24dp" />

    </LinearLayout>

<TextView
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/horizontalScrollView"
    android:text="$39.99"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="20sp"
    android:textColor="@color/colorAccent"
    android:textStyle="bold"
    />


</LinearLayout>

解决方法 1:

在构造函数中表现我们检查,看看是否我们能,我们将使用先前 layout 已生成,只是试图取代它们的值,但你的情况因为你不能改变的 scrol 栏 view 所以你的新项目和以前的项目会有相同的滚动条 view .但愿我所以你要始终生成新 layout 喜欢这清楚。:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);

// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag

final View result;


    viewHolder = new ViewHolder();
    LayoutInflater inflater = LayoutInflater.from(getContext());
    convertView = inflater.inflate(R.layout.row_item, parent, false);
    viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
    //viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
    viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
    viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);

    result = convertView;

    convertView.setTag(viewHolder);

viewHolder.itemTitle.setText(item.getTitle());
viewHolder.itemPrice.setText("$" + item.getPrice());
viewHolder.storeLogo.setOnClickListener(this);
viewHolder.storeLogo.setTag(position);

// Return the completed view to render on screen
return convertView;

}

也或许有更好的方式,不知道给什么样的男人,但这至少会解决你的问题。

更新︰ 有关如何详细信息 listview 工作和性能提示此链接起来的好

赞助商