用recycleview打造一个瀑布流效果

用recycleview打造一个瀑布流效果

用recycleview打造一个瀑布流效果

MainActivity

package wiki.bfw.weixin;

import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private FriendsAdapter mAdapter;
    private List<Integer> mDatas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        initDatas();
        //得到控件
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
        //设置布局管理器
        StaggeredGridLayoutManager stagegridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
       // stagegridLayoutManager.setOrientation(stagegridLayoutManager.VERTICAL);

        //LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
       // linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(stagegridLayoutManager);
        //设置适配器
        mAdapter = new FriendsAdapter(this, mDatas);
        mAdapter.setOnItemClickLitener(new FriendsAdapter.OnItemClickLitener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
                Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        mRecyclerView.setAdapter(mAdapter);


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    private void initDatas()
    {
        mDatas = new ArrayList<Integer>(Arrays.asList(R.drawable.ic_launcher_background, R.mipmap.demo,R.mipmap.demo,R.drawable.ic_launcher_background,R.mipmap.demo,R.mipmap.demo,R.drawable.ic_launcher_background,R.mipmap.demo,R.drawable.ic_launcher_background,R.mipmap.demo,R.mipmap.demo,R.mipmap.demo));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
     ...

点击查看剩余70%

{{collectdata}}

网友评论0