1

我有一个带有 baseadapter 的菜单列表视图。

我的活动:

listView_menu = (ListView) findViewById(R.id.listView_menu);
model_category = new Model_Category(context);
listView_menu.setAdapter(new BaseAdapter_Menu(context, model_category.GetAllDifferentCategory()));
listView_menu.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
        {
            view.setSelected(true);
        }
    });
listView_menu.setSelection(0);

我的 BaseAdapter_Menu 的 getView 方法(扩展 BaseAdapter):

@Override
public View             getView(int position, View convertView, ViewGroup parent)
{
    View                rowView = convertView;

    if (rowView == null)
    {
        rowView = this.inflater.inflate(R.layout.customitemlistview_menu, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.category = (TextView) rowView.findViewById(R.id.category_menu);
        rowView.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder)rowView.getTag();
    holder.category.setText(this.data.get(position));
    return (rowView);
}

每个项目都有一个背景选择器,根据其状态(SELECTED 或 NOT)有 2 个不同的背景。

当我单击列表视图中的某个项目时,我设置为我的视图item.setSelected("true")

因此,当我单击时,项目的背景会发生变化,但是如何在不单击的情况下更改列表视图的第一项的背景。我已经尝试过“listView_menu.setSelection(0)”,但它不起作用。

4

4 回答 4

2

我找到了解决方案!:)

在我的 onCreate 中,我应用了这段代码:

listView_menu.setAdapter(new BaseAdapter_Menu(context, model_category.GetAllDifferentCategory()));
    listView_menu.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id)
        {
            ((BaseAdapter_Menu) listView_menu.getAdapter()).setPositionSelected(position);
        }
    });

在我的 BaseAdater_Menu :

我创造private int PositionSelected = 0;

我添加了这个方法

public void setPositionSelected(int position)
{
    PositionSelected = position;
    this.notifyDataSetChanged();
}

我覆盖了 getView 方法

public View getView(int position, View convertView, ViewGroup parent)
{
    View rowView = convertView;

    if (rowView == null)
    {
        rowView = this.inflater.inflate(R.layout.customitemlistview_menu, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.category = (TextView) rowView.findViewById(R.id.category_menu);
        rowView.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder)rowView.getTag();
    holder.category.setText(this.data.get(position));
    if (position == PositionSelected)
        rowView.setBackgroundResource(R.drawable.item_menu_selected_background);
    else
        rowView.setBackgroundResource(R.drawable.item_menu_background);
    return (rowView);
}

谢谢您的帮助 :)

于 2013-02-04T15:22:29.350 回答
1

编辑:

它是一种解决方法,但我认为它有效:保存 FisrtTime 的变量,它将在创建后立即选择视图(在 BaseAdapter_Menu 上)

public BaseAdapter_Menu(...){
    ....
    mFirstTime = true;
    ....
}

public getView(int position, ...){
    .... // create your view

    if (mFirstTime && position == 0){
         mFirstTime = false;
         rootView.setSelected(true);
    }

    ... //Setup your view
}
于 2013-02-04T10:38:00.260 回答
0

res/layout-v11/cell.xml

如果您以后只需要开发 API 级别 11 的应用程序,您所要做的就是编写如下 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="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:orientation="vertical">
    <TextView 
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

资源/布局/cell.xml

但是,在 API 级别 <= 10 中,上述代码会崩溃。所以它需要像以下......

<?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="wrap_content"
    android:orientation="vertical">
    <TextView 
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

DataListCursorAdapter.java

其次,需要通知您的适配器分配数据。//...(a)

class DataListCursorAdapter extends CursorAdapter {
    private Listener mListener;
    public static class ViewHolder {
        public TextView name;
        public TextView text;
    }
    @SuppressWarnings("deprecation")
    public DataListCursorAdapter(Context context, Cursor c, Listener listener) {
        super(context, c);
        mListener = listener;
    }
    public DataListCursorAdapter(Context context, Cursor c, Listener listener, boolean autoRequery) {
        super(context, c, false);
        mListener = listener;
    }
    public DataListCursorAdapter(Context context, Cursor c, Listener listener, int flags) {
        super(context, c, FLAG_REGISTER_CONTENT_OBSERVER);
        mListener = listener;
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        try {
            ViewHolder holder = (ViewHolder) view.getTag();
            JSONObject json = new JSONObject(cursor.getString(cursor.getColumnIndex(Table.DATA)));
            final Data data = new Data(json);
            mListener.onDataAssigned(data, view);//...(a)

            holder.name.setText(data.name);
            holder.text.setText(data.text);
        }
        catch (JSONException e) {
        }
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.cell, null);
        ViewHolder holder = new ViewHolder();
        holder.name      = (TextView) view.findViewById(R.id.name);
        holder.text      = (TextView) view.findViewById(R.id.text);
        view.setTag(holder);
        return view;
    }
    public interface Listener {
        public void onDataAssigned(Data data, View view);//...(a)
    }
}

YourFragment.java 或 YourActivity.java

您实现接口并...

@Override
public void onDataAssigned(Data data, View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        return;
    }
    if (TextUtils.equals(mLastSelectedDataId, data.id)) {// default color
        view.setBackgroundColor(0xfff7aecb);
    }
    else {// selected color
        view.setBackgroundColor(0x00000000);
    }
}

您将所选项目保留在班级的字段中。

@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
    Cursor cursor = mArrayCursorAdapter.getCursor();
    JSONObject json = new JSONObject(cursor.getString(cursor.getColumnIndex(Table.DATA)));
    final Data data = new Data(json);
    if (data != null && TextUtils.equals(mLastSelectedDataId, data.id)) {
        getListView().setItemChecked(position, false);
        mLastSelectedDataId = null;
    }
    else if (data != null) {
        getListView().setItemChecked(position, true);
        mLastSelectedDataId = data.id;
    }
}

重要的

  • 在列表视图上,项目视图的数量不是项目的数量。
  • 已更改颜色的选定视图被重新用于其他数据。

更新

public class YourAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Listener mListener;
    private Model_Category mData;

    static class ViewHolder {
        TextView category;
    }
    public YourAdapter(Context context, Model_Category data, LayoutInflater inflater, Listener listener) {
        mInflater = inflater;
        mListener = listener;
        mData = data;
    }
    @Override
    public int getCount() {
        return /*return count*/;
    }
    @Override
    public Object getItem(int position) {
        return /*return item by position*/;
    }
    @Override
    public long getItemId(int position) {
        return /*return id by position*/;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null){
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.customitemlistview_menu, null);
            holder.category = (TextView) convertView.findViewById(R.id.category_menu);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        mListener.onDataAssigned(mData.get(position), convertView);//...(a)
        holder.category.setText(mData.get(position));
        return convertView;
    }
    public interface Listener {
        public void onDataAssigned(Data data, View view);//...(a)
    }
}
于 2013-02-01T17:34:37.463 回答
0

getView()在您的方法中添加此代码:

if(position == 0) {
   rowView.setClickable(true);
   rowView.setSelected(true);
}

编辑 :

Activity应该实现OnItemClickListener这样的:

public class YourActivity extends Activity implements OnItemClickListener {

onCreate()在你的方法中试试这个:

//after setting the onItemClickListener to your listView : 
listView_menu.setOnItemClickListener(this);
listView_menu.setSelection(0);
View firstView = listView_menu.getChildAt(0);
onItemClick(listView_menu, firstView, 0, -1);

并将方法提取onItemClick()到您的活动中:

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
     view.setSelected(true);
}
于 2013-02-01T16:46:35.607 回答