从ListView行获取视图

时间:2015-08-11 18:05:36

标签: android listview android-arrayadapter android-listfragment

基本上,当我选择行时,我尝试做的就是在一行中出现一个按钮。我尝试了很多方法,它们似乎都在工作,包括下面的方法......我得到ImageView但是调用setVisibility()就没有了:

ListFragment

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
    setSellButton();
}

public View getViewForPosition(int position){
    int relativePos = position - listview.getFirstVisiblePosition();
    if( relativePos < 0 || relativePos > listview.getChildCount()){
        return null;
    }
    return listview.getChildAt(relativePos);
}

public void setSellButton() {
    View x = getViewForPosition(Constants.lastSelection);
    ImageView y = (ImageView)x.findViewById(R.id.ivSell);
    y.setVisibility(View.VISIBLE);
}

没有错误,调试显示setSellButton()中的所有内容都是正确的,但setVisibility()永远不会有效。这是我只能在适配器中做的事情吗?谢谢!



修改:按要求排列XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:background="@drawable/thelist"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivStar"
        android:src="@drawable/liststar"
        android:layout_weight="1" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_weight="10">

        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textStyle="bold">
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvValue"
        android:textSize="13sp" />

</LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coin"
        android:id="@+id/ivSell"
        android:visibility="invisible" />

</LinearLayout>

修改:适配器

public class InventoryAdapter extends ArrayAdapter<Integer> {

    private final Context context;
    private final ArrayList<String> names;
    private final ArrayList<Integer> stock;
    LayoutInflater inflater;
    View rowView;
    String stockText;
    TextView textView;
    TextView textView2;
    ImageView iv;

    public InventoryAdapter(Context context, ArrayList<String> names, ArrayList<Integer> stock) {
        super(context, R.layout.inv_rowlayout, stock);
        this.context = context;
        this.names = names;
        this.stock = stock;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.inv_rowlayout, parent, false);

        String stockNumber = stock.get(position).toString();
        final String name = names.get(position);

        textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names.get(position));
        textView2 = (TextView) rowView.findViewById(R.id.tvValue);
        textView2.setText(stockNumber);
        iv = (ImageView)rowView.findViewById(R.id.ivSell);

        return rowView;
    }

}

3 个答案:

答案 0 :(得分:1)

我的猜测是,在单击该项后,您需要在适配器上调用notifyDataSetChanged方法。然后在所选列表视图项上显示按钮。

答案 1 :(得分:1)

好。首先允许我调整你的行xml。最好使用RelativeLayout代替LinearLayout。它更灵活。

<强> row_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/thelist"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:minHeight="@dimen/listView_minHeight">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivStar"
        android:contentDescription="@string/list_item_image_desc"
        android:layout_gravity="center_vertical"
        android:layout_centerVertical="true"
        android:src="@drawable/liststar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/ivStar"
        android:layout_toEndOf="@+id/ivStar"
        android:layout_alignParentTop="true"
        android:id="@+id/label"
        android:text="New Text"
        android:textSize="15sp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize="13sp"
        android:id="@+id/tvValue"
        android:layout_below="@+id/label"
        android:layout_toRightOf="@+id/ivStar"
        android:layout_toEndOf="@+id/ivStar"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivSell"
        android:src="@drawable/coin"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:visibility="invisible" />

</RelativeLayout>

你可能是BaseAdapter的子类。您需要做的是在那里创建一个方法来更改ImageView的可见性。然后,当您单击项目时,可以获取adapter并调用该方法。

<强> CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    private Context context_;
    private ArrayList<YourDataHolder> items;
    private YourDataHolder lastHolder = null;

    private final String LOG_TAG = CustomAdapter.class.getSimpleName();

    public CustomAdapter(Context context, ArrayList<YourDataHolder> items) {
        this.context_ = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context_.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.row_layout, null);
        }

        final TextView tv_label = (TextView) convertView.findViewById(R.id.label);
        final TextView tv_value = (TextView) convertView.findViewById(R.id.tvValue);
        final ImageView iv_star = (ImageView) convertView.findViewById(R.id.ivSell);

        YourDataHolder holder = items.get(position);

        tv_label.setText(holder.getLabel());
        tv_value.setText(holder.getValue());

        if (holder.isImageVisible() && iv_star.getVisibility() == ImageView.INVISIBLE) {
            iv_star.setVisibility(ImageView.VISIBLE);
        }
        else if (iv_star.getVisibility() == ImageView.VISIBLE) {
            iv_star.setVisibility(ImageView.INVISIBLE);
        }


        return convertView;
    }

    public void onItemSelect(int position) {
        if (position < items.size()) {
            YourDataHolder holder = items.get(position);

            holder.setVisibility(true);

            if (lastHolder != null) {
                lastHolder.setVisibility(false);
            }

            lastHolder = holder;
        }
    }
}

假设你正在使用像这样的数据持有者:

<强> YourDataHolder.java

public class YourDataHolder {
    private String label;
    private String value;
    private boolean isVisible;

    public YourDataHolder(String label, String value) {
        this.label = label;
        this.value = value;
        isVisible = false;
    }

    public String getLabel() {
        return label;
    }

    public String getValue() {
        return value;
    }

    public boolean getVisibility() {
        return isVisible;
    }

    public void setVisibility(boolean isVisible) {
        this.isVisible = isVisible;
    }
}

然后,在你的onItemClick()上,做一下魔术。

<强> YourFragment.java

private CustomAdapter adapter;

// Rest of the Fragment. Where you create the adapter, and assign it to the ListView.

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
    adapter.onItemSelect(position);
    adapter.notifyDataSetChanged();
}

答案 2 :(得分:0)

我将在适配器中执行此操作,直接访问imageview将使该过程更容易。我之前尝试从列表视图级别执行此操作,但无法完成我设置的条件。