使用addHeaderView()时Listview项选择器问题

时间:2015-02-27 16:40:56

标签: android android-listview

我在Listview上添加了静态布局,并使用

禁用了headerview
View view = View.inflate(MainActivity.this, R.layout.drawer_header,
            null);
    mDrawerList.addHeaderView(view, null, false);//mDrawerList is Listview

为了避免项目位置问题,我添加了

   private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        position -= mDrawerList.getHeaderViewsCount();
        Log.i("TAG POSITION", String.valueOf(position));
        displayView(position);
    }
}

在Listview onitemclick中。现在根据Listview项加载正确的片段。但我有另一个list_selector问题。抽屉Listview是

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="270dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

当我点击位置&#39; 1&#39;然后list_selctor应用于位置&#39; 0&#39;,如果我点击位置&#39; 2&#39;然后list_selector应用于位置&#39; 1&#39;等等...

适配器类..

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    //ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

    //imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

}

请帮忙解决这个问题

1 个答案:

答案 0 :(得分:0)

即使我的标题不可点击,我也没有遇到任何问题。尝试删除它并显示列表中的项目:

    position -= mDrawerList.getHeaderViewsCount();