接口

来源:互联网 发布:推广优化软件 编辑:程序博客网 时间:2024/06/05 08:06

//分割线的属性

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <gradient android:centerColor="#ff00ff00"        android:endColor="#ff0000ff"        android:startColor="#ffff0000"        android:type="linear"        >    </gradient> <size android:height="4dp"></size></shape>

//styles

<resources>    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="android:listDivider">@drawable/fgx</item>    </style></resources>
mainactivity

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="test.bwei.com.jiekouhuidiao.MainActivity">    <Button        android:id="@+id/additem"        android:text="添加条目"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <android.support.v7.widget.RecyclerView        android:id="@+id/rv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/additem" /></RelativeLayout
//item

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/text222"        android:text="9999999999"        android:layout_width="match_parent"        android:layout_height="30dp"        android:gravity="center"/></RelativeLayout>
//MainActivity

package com.example.administrator.cr_js927;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.DividerItemDecoration;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    //定义属性    RecyclerView   rv;    List<String>   list;    Button         add;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取属性的地址        rv=(RecyclerView) findViewById(R.id.rv);        add=(Button) findViewById(R.id.additem);        //调用方法        initRV();    }    //创建方法    public void initRV(){        //调用集合的方法        initData();        //管理器        LinearLayoutManager manager=new LinearLayoutManager(this);        rv.setLayoutManager(manager);        //实例化适配器        final Myadapter  adapter=new Myadapter();        rv.setAdapter(adapter);        //添加分割线        rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));        //条目的点击事件删除条目        adapter.setOnItemClickLinstener(new OnItemClickLinstener() {            @Override            public void OnItemClick(View view, int position) {                list.remove(position);                adapter.notifyDataSetChanged();//刷新                Toast.makeText(MainActivity.this,position+"已经被删除!!!",Toast.LENGTH_SHORT).show();            }        });        //长按的点击事件        adapter.setOnLongClickListener(new OnItemLongClickListener() {            @Override            public void OnItemlongClick(View view, int position) {                Intent    in=new Intent(MainActivity.this,Main2Activity.class);                startActivity(in);            }        });        //按钮的点击事件添加条目        add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                list.add(0,"new CR");                adapter.notifyDataSetChanged();                Toast.makeText(MainActivity.this,"您添加了新的条目!!!",Toast.LENGTH_SHORT).show();            }        });    }    //集合的添加方法    private void initData() {        list=new ArrayList<>();        for (int i=0;i<50;i++)        {            list.add("position"+i);        }    }    //RecyclerView创建适配器    class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder>{        //接口的方法实例化        OnItemClickLinstener   listener;        OnItemLongClickListener    loostener;        //获取方法的传参值        public void setOnItemClickLinstener(OnItemClickLinstener listener) {            //指定当前的值            this.listener = listener;        }        public void setOnLongClickListener(OnItemLongClickListener lootener) {            //指定当前的值            this.loostener=lootener;        }        @Override        public MyViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {           View view=View.inflate(MainActivity.this,R.layout.item,null);            MyViewHolder holder=new MyViewHolder(view);            //页面的点击事件            view.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    //实例化listener来调用接口的方法    强转类型                    listener.OnItemClick(view,(int)view.getTag());                }            });            //页面的长按点击事件            view.setOnLongClickListener(new View.OnLongClickListener() {                @Override                public boolean onLongClick(View view) {                    loostener.OnItemlongClick(view,(int)view.getTag());                    return true;                }            });            return holder;        }        @Override        public void onBindViewHolder(MyViewHolder holder, int position) {               holder.text.setText(list.get(position));            //设置接口方法的传参值               holder.itemView.setTag(position);        }        @Override        public int getItemCount() {            return list==null ? 0 : list.size();        }        //创建的MyViewHolder        class MyViewHolder extends RecyclerView.ViewHolder{                 TextView  text;            public MyViewHolder(View itemView) {                super(itemView);                text=(TextView)itemView.findViewById(R.id.text222);            }        }    }    //创建接口    public  interface OnItemClickLinstener{        //创建接口的方法        public void OnItemClick(View view,int position);    }    public  interface OnItemLongClickListener{        //创建接口的方法        public void OnItemlongClick(View view,int position);    }}