单击DrawerLayout滑动菜单中的一行时如何使视图可见?

时间:2014-04-29 09:25:22

标签: android listview adapter drawerlayout

我使用DrawerLayout生成了Sliding ListView。我想知道如何做以下事情。 1.如何获取Slider ListView的选定行ID。 2.如何在单击或选择Slider Listview行时显示具有某种颜色的视图。 3.如何在滑动列表中显示drawable图像。

我在SD卡生成的第一行上有图片。我想把drawable文件夹中的其他图片显示出来。我的代码如下:

Profile.class

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);  
    dataList = new ArrayList<DrawerItem>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
            dataList.add(new DrawerItem(c.get("username"),bmp));

    //dataList.add(new DrawerItem(c.get("username"),R.drawable.ic_action_good));
    dataList.add(new DrawerItem("contacts", R.drawable.ic_action_good));
    dataList.add(new DrawerItem("Tasks Sent by Me", R.drawable.ic_action_gamepad));
    dataList.add(new DrawerItem("My Tasks", R.drawable.ic_action_labels));
    dataList.add(new DrawerItem("Profile", R.drawable.ic_action_search));
    dataList.add(new DrawerItem("Settings", R.drawable.ic_action_cloud));
            adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
            dataList);

    mDrawerList.setAdapter(adapter);

CustomdrawerAdapter.class

     public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

Context context;
List<DrawerItem> drawerItemList;
int layoutResID;

public CustomDrawerAdapter(Context context, int layoutResourceID,
        List<DrawerItem> listItems) {
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.drawerItemList = listItems;
    this.layoutResID = layoutResourceID;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
       drawerHolder.vieworange=(View)view.findViewById(R.id.vieworangelist);

        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
    Log.e("drawer position",""+dItem);

    /*drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
            dItem.getImgResID()));*/
    //drawerHolder.ItemName.
    drawerHolder.ItemName.setText(dItem.getItemName());
    drawerHolder.icon.setImageBitmap(dItem.bmp);

    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
    View vieworange;
}
    }

1 个答案:

答案 0 :(得分:1)

试试这样..

drawerListRight.setOnItemClickListener(new RightDrawerItemClickListener());

现在你的点击监听器就像这样..

private class RightDrawerItemClickListener implements
            ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            // When clicked, select open the appropriate fragment
            selectItemRight(position); // Need to change same for right

        }

    }

然后..

private void selectItemRight(int position) {

        switch (position) {
        case 0: {
            Intent intent = new Intent(DashBoardMain.this, UserProfile.class);
            startActivityForResult(intent, 10);
            break;
        }
        case 1: {
            Intent intent = new Intent(DashBoardMain.this, BookingHistory.class);
            startActivity(intent);
            break;
        }
        case 2: {

            /*
             * Intent intent = new Intent(DashBoardMain.this,
             * PriceChartMain.class); startActivity(intent);
             */

            if (prm.status) {
                Intent intent = new Intent(DashBoardMain.this,
                        PriceChartMain.class);
                intent.putExtra("complete_response", myjsonString);
                intent.putExtra("cityId", pass_city_id);
                startActivity(intent);

            } else {
                NativeAlertDialog nad = new NativeAlertDialog();
                nad.showNativeAlertDialog(DashBoardMain.this, "Price Chart",
                        "No service available. Contact Eco Cabs for further support.");
            }

            break;
        }
        case 3: {
            Intent intent = new Intent(DashBoardMain.this, About.class);
            startActivity(intent);
            break;
        }


        }

        drawerListRight.setItemChecked(position, true);

    }

修改

您的适配器看起来像..

private MenuListAdapterRight menuAdapterRight;
private int[] icons;
private final String[] titles = new String[] { "PROFILE",
            "BOOKING HISTORY", "PRICE CHART", "ABOUT", "SHARE" };

现在使用OnCreate方法..

// Get the icons from the drawables folder
        icons = new int[] { R.drawable.profile_b, R.drawable.booking_history_b,
                R.drawable.price_chart_b, R.drawable.about_b,
                R.drawable.share_b };

现在设置这样的适配器..

drawerListRight = (ListView) findViewById(R.id.listview_drawer_two); // Right

        menuAdapterRight = new MenuListAdapterRight(DashBoardMain.this, titles,
                icons);
        drawerListRight.setAdapter(menuAdapterRight);
        drawerListRight
                .setOnItemClickListener(new RightDrawerItemClickListener());

如上所述休息其他......

希望它有所帮助..祝你好运!