如何根据从同一listView中的项目触发的操作禁用/启用listview项目

时间:2015-01-12 10:48:22

标签: android listview android-listview navigation-drawer

我有一个navigationDrawer,其listView有三个项目(item1,item2和item3)。 item1,在其布局中有两个按钮(启用和禁用)。我想要做的是,当用户点击item1然后点击布局中的启用按钮时,navigationdrawer的列表视图中的其他两个项目应该出现。如果用户单击禁用按钮,(item2和item3)如果出现则应该消失。

目前,我被卡在button listener中的item1 layout,我不知道在item1布局的button listener内写什么。亲切的租赁有一个乐趣。

下面是我的尝试,请为此我需要帮助和指导。

mainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initViews(R.layout.activity_main);
    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displaySelectionLayout(0);
    }
}

private void displaySelectionLayout(int position) {
    // TODO Auto-generated method stub
    Fragment fragment = null;

    switch (position) {

    case 0:
        fragment = new Item1_Fragment();
        break;
    case 1:
        fragment = new Item2_Fragment();
        break;
    case 2: fragment = new Item3_Fragment();

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

        navDrawerListView.setItemChecked(position, true); // update selected item and title, then close the drawer
        navDrawerListView.setSelection(position);
        setTitle(navDrawerOptions[position]);
        navDrawerLayout.closeDrawer(navDrawerListView);
    } else {
        Log.e(TAG, "Error in creating fragment"); // error in creating fragment
    }
}

private void initViews(int activityMain) {
    // TODO Auto-generated method stub
    setContentView(R.layout.activity_main);
    navDrawerLayout = (DrawerLayout) findViewById(R.id.navDrawerLayout);
    navDrawerListView = (ListView) findViewById(R.id.navDrawerListView);
    navDrawerOptions = getResources().getStringArray(R.array.nav_drawer_items);

    navDrawerArrayList = new ArrayList<NavDrawerItemStructure>();
    navDrawerArrayList.add(new NavDrawerItemStructure(navDrawerOptions[0], VISIBE));
    navDrawerArrayList.add(new NavDrawerItemStructure(navDrawerOptions[1], VISIBE));
    navDrawerArrayList.add(new NavDrawerItemStructure(navDrawerOptions[2], VISIBE));

    adapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerArrayList);
    navDrawerListView.setAdapter(adapter);
    navDrawerListView.setOnItemClickListener(new SlideMenuClickListener());

    setUpNavDrawer();
}

private void setUpNavDrawer() {
    // TODO Auto-generated method stub      
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    mTitle = mDrawerTitle = getTitle();

    navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout,
            R.drawable.ic_launcher, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ){
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // calling onPrepareOptionsMenu() to show action bar icons
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // calling onPrepareOptionsMenu() to hide action bar icons
        }
    };
    navDrawerLayout.setDrawerListener(navDrawerToggle);
}

/**
 * Slide menu item click listener
 * */
class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // display view for selected nav drawer item
        displaySelectionLayout(position);
    }
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = navDrawerLayout.isDrawerOpen(navDrawerListView);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (navDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    navDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    navDrawerToggle.onConfigurationChanged(newConfig);
}

}

item1_Fragement

public class Item1_Fragment extends Fragment{

private Button btn_enable;
private Button btn_disable;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View rootView = inflater.inflate(R.layout.item1_layout, container, false);
    btn_enable = (Button) rootView.findViewById(R.id.btn_enable);
    btn_disable = (Button) rootView.findViewById(R.id.btn_disable);
    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    btn_enable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // how can i dispatch the action from item1 listener to the mainactivit??
        }
    });
}

}

NavDrawer适配器类

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItemStructure> itemsList;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItemStructure> itemsList) {
    // TODO Auto-generated method stub
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.itemsList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return this.itemsList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_view_item_design, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.tv_itemTitle);
    tv.setText(this.itemsList.get(position).getTitle());
    if (!this.itemsList.get(position).getVisibility()) {
        tv.setVisibility(View.GONE);
    }
    return convertView;
}

}

1 个答案:

答案 0 :(得分:0)

之前我遇到过这个问题,我认为你可以做到以下几点:

1-实现一个接口,如:

public interface DataPasserInterface {

public void passItemsVisibility(boolean item2_Visibility, boolean item3_visibility);

}

2 - 在您的点击监听器中,您可以执行类似下面发布的操作,当然您可以操作布尔参数以满足您的要求:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    btn_enable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dataPasser.passItemsVisibility(true, true);
        }
    });

    btn_disable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dataPasser.passItemsVisibility(false, false);
        }
    });

3 - 主要活动的标题应该是

public class MainActivity extends Activity implements DataPasserInterface {

4-eclipse可以自动将未实现的方法添加到您的主要活动中,然后,您只需要实现它们。

@Override
public void passItemsVisibility(boolean item2_Visibility, boolean item3_visibility) {
    // TODO Auto-generated method stub
    Log.d(TAG, "item2_Visibility: " + item2_Visibility);
    Log.d(TAG, "item3_visibility: " + item3_visibility);
}