简体   繁体   English

如何从菜单列表中使用RecycleView Android进行gridlayout和linearlayout

[英]How to have gridlayout and linearlayout using RecycleView Android from Menu list

I have a list but I want to make an option for the users to view it in List or Grid. 我有一个列表,但是我想为用户提供一个选项,以便在列表或网格中查看它。 The choices are in the menu. 选项在菜单中。 Here's the menu_user_list.xml 这是menu_user_list.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.user.UserListActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_listview" android:title="List" app:showAsAction="never" android:onClick="showlist" android:orderInCategory="200"/>
<item android:id="@+id/action_gridview" android:title="Grid" app:showAsAction="never" android:onClick="showgrid" android:orderInCategory="300"/>

How could I do this? 我该怎么办?

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_user_list_recyclerView);

The R.id.activity_user_list_recyclerView is located on the Activity layout. R.id.activity_user_list_recyclerView位于“活动”布局上。 It is working fine there. 那里工作正常。 My main point is that how could set the menu that it will change the layout to either of the these two: 我的主要观点是如何设置菜单以将布局更改为以下两个菜单之一:

recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setLayoutManager(new LinearLayoutManager(this));

The adapter works fine. 适配器工作正常。 I just need to figure out how the onclick will change the layout. 我只需要弄清楚onclick将如何更改布局。 Thanks 谢谢

If you are using a Toolbar but not set as the ActionBar with AppCompat you would handle this by adding a menu to your toolbar: 如果您使用的是工具栏,但未通过AppCompat设置为ActionBar,则可以通过向工具栏添加菜单来解决此问题:

 Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
 toolbar.inflateMenu(R.menu.my_menu);

 // Then set an OnMenuItemClickListener
 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            int itemId = item.getItemId();

            switch(itemId){

                case R.id.action_settings:

                    // handle settings
                    break;

                case R.id.action_listview:

                    // Setup the LinearLayoutManager
                    initListDisplay();
                    break;


                case R.id.action_gridview:

                    // Setup the GridLayoutManager
                    initGridDisplay();
                    break; 
            }

            return true;
        }
    });

if your using the toolbar as your ActionBar ie setSupportActionBar(toolbar); 如果您使用工具栏作为ActionBar,即setSupportActionBar(toolbar); then you would just inflate the menu regularly and add onOptionsItemSelected(MenuItem): 那么您只需定期为菜单充气并添加onOptionsItemSelected(MenuItem):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    switch(id){

        case R.id.action_settings:

            // handle settings
            break;

        case R.id.action_listview:

            // Setup the LinearLayoutManager
            initListDisplay();
            break;


        case R.id.action_gridview:

            // Setup the GridLayoutManager
            initGridDisplay();
            break;

    }
    return super.onOptionsItemSelected(item);
}

Then i would use something like this to do the work: 然后,我将使用类似的方法进行工作:

// Display a list
private void initListDisplay(){
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

// Display the Grid
private void initGridDisplay(){
    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    layoutManager.setOrientation(GridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
}

For simplicity, i have left out the few other options you have to inflate menus with ActionBar or ActionBarSherlock, but the onMenuItemClick is pretty standard so, just use what you can with the switch statement. 为简单起见,我省略了一些其他必须使用ActionBar或ActionBarSherlock来膨胀菜单的选项,但是onMenuItemClick是非常标准的,因此,只需使用switch语句即可。

Good Luck and Happy Coding! 祝您好运,编码愉快!

In the click listener, just set the new layout manager and re-set the adapter: 在点击侦听器中,只需设置新的布局管理器并重新设置适配器即可:

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

For code optimization, you can add a boolean variable in your class, to know which LayoutManager is already used. 对于代码优化,您可以在类中添加一个布尔变量,以了解已经使用了哪个LayoutManager

eg: 例如:

case R.id.menu_switch_view:
     isGridLayout = !isGridLayout;
     recyclerView.setLayoutManager(isGridLayout ? new GridLayoutManager(this, 2) : new LinearLayoutManager(this));
     recyclerView.setAdapter(mAdapter);
break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM