万能适配器与ToolBar+DrawerLayout

万能适配器

ViewHolder类

    public class BaseVrHolder extends RecyclerView.ViewHolder {
    
        SparseArray<View> array = new SparseArray<>();
        Context context;
        public BaseVrHolder(@NonNull View itemView, Context context) {
            super(itemView);
            t

    his.context=context;
        }
    public View getview(int id){
        View view = array.get(id);
        if (view==null){
         view = itemView.findViewById(id);
         array.put(id,view);
        }
        return view;
    }

    public void setText(int id,String str){
        TextView textView = (TextView) getview(id);
        if (textView!=null){
            textView.setText(str);
        }
    }

    public void setamg(int id,String pic){
        ImageView imageView = (ImageView) getview(id);
        if (imageView!=null){
            Picasso.with(context).load(pic).into(imageView);
        }
    }
    }

适配器类

public abstract class BaseRvAdapter<T> extends RecyclerView.Adapter<BaseVrHolder> {

    int layoutId;
    List<T> list = new ArrayList<>();
    Context context;
    public BaseRvAdapter(int layoutId, Context context, List<Map<String, Object>> list) {
        this.layoutId = layoutId;
        this.context= context;
        this.list= (List<T>) list;
    }

    @NonNull
    @Override
    public BaseVrHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.name1,viewGroup,false);
        return new BaseVrHolder(view,context);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseVrHolder baseVrHolder, int i) {
        bind(baseVrHolder,i);
    }

    public abstract void bind(BaseVrHolder holder, int i);

    @Override
    public int getItemCount() {
        return list.size();
    }
    }

主类

主类布局文件

<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

</LinearLayout>

自定义布局

<LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>

主类

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    BaseRvAdapter<Map<String, Object>> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rv3);
        String str = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
        List<Map<String, Object>> list = new ArrayList<>();

        new MyThread(str,list).start();
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(manager);

        adapter = new BaseRvAdapter<Map<String, Object>>(R.layout.name1, this, list) {
            @Override

public void bind(BaseVrHolder holder, int i) {

            holder.setText(R.id.t1,list.get(i).get("title").toString());
            holder.setamg(R.id.iv1,list.get(i).get("pic").toString());
        }
    };
    recyclerView.setAdapter(adapter);


}
}

效果图
在这里插入图片描述

ToolBar+DrawerLayout布局文件

<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Demo">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/bar"
        android:background="#FF0066FF"
        app:logo="@android:drawable/presence_online"
        app:title="标题"
        app:titleTextColor="#fff"
        app:subtitle="副标题"
        app:subtitleTextColor="#fff"
        app:navigationIcon="@android:drawable/presence_busy"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="请输入内容"
                android:drawableLeft="@android:drawable/presence_offline"
                />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/dra"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff0"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="这是主界面"
                android:textColor="#000"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#f00"
            >
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="左侧视图"
                />
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

主类

public class Demo extends AppCompatActivity {

    Toolbar toolbar;
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        init();
        initdra();
    }

    public void init(){
        toolbar = findViewById(R.id.bar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Demo.this, "点击导航栏", Toast.LENGTH_SHORT).show();
            }
        });
        setSupportActionBar(toolbar);//设置使用当前。替换activityBar
    }
public void initdra(){
    drawerLayout = findViewById(R.id.dra);
    //绑定toolbar和drawerlayout
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openlayout,R.string.closelayout);
    toggle.syncState();
    drawerLayout.addDrawerListener(toggle);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    switch (itemId){
        case R.id.menu1:
            Toast.makeText(this, "点击菜单", Toast.LENGTH_SHORT).show();
            break;
        case R.id.son1:
            if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                drawerLayout.closeDrawer(Gravity.LEFT);
            }else{
                drawerLayout.openDrawer(Gravity.LEFT);
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}

}

效果图

在这里插入图片描述



猜你喜欢

转载自blog.csdn.net/wangwei_weibo/article/details/91356128