这里总供给讲解两种方法:
一:利用依赖:
1、首先在项目的build下注册
allprojects {
repositories {
jcenter()
//下面这句才是加上的,上面的自带 的
maven {url "https://jitpack.io"}
}
}
2、然后在app下的build中导入依赖
compile 'com.github.andyoom:draggrid:v1.0.1'
3、然后在main类中就可以使用了,只要用集合将数据添加就可以了
public class MainActivity extends AppCompatActivity {
private List<ChannelBean> list;
private String jsonStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
4、在点击事件里做逻辑处理
//按钮点击跳转到控制界面
public void button(View view){
if (list==null){//判断集合中是否已有数据,没有则创建
list=new ArrayList<>();
//第一个是显示的条目,第二个参数是否显示
list.add(new ChannelBean("热点",true));
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("八卦",true));
list.add(new ChannelBean("游戏",true));
list.add(new ChannelBean("宠物",true));
list.add(new ChannelBean("汽车",false));
list.add(new ChannelBean("热卖",false));
list.add(new ChannelBean("外卖",false));
list.add(new ChannelBean("太阳花",false));
list.add(new ChannelBean("九三",false));
list.add(new ChannelBean("八嘎",false));
list.add(new ChannelBean("色昂",false));
ChannelActivity.startChannelActivity(this,list);
}else if (jsonStr!=null){//当判断保存的字符串不为空的时候,直接加载已经有了的字符串
ChannelActivity.startChannelActivity(this,jsonStr);
}
}
5、然后再回调一下
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==ChannelActivity.REQUEST_CODE&&resultCode==ChannelActivity.RESULT_CODE) {
jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
} }
}
注意:
如果run之前程序报错如:
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
即依赖冲突,根据报错的详细信息查找冲突的类。
例如:
报错:
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/gson/annotations
这时候我们可以发现
com/google/gson/annotations/Expose.class
最后面有一个Expose.class
我们可以通过Search Everywhere去定位这个类(默认快捷键是双击shift)。
看,通过搜索的确发现了两个一模一样的类,而且来自不同的依赖,而重复的类库正是gson。
然后我们只要删除一个架包或者依赖就可以了,最好是删除外面的单独架包,比如我们自己导入的Gson包,就可以解决问题了。
二:自己自定义处理逻辑:
主要逻辑就是:
创建两个GridView,分别添加数据,并添加item的点击事件,在点击上面的
GridView的时候删除这个条目,并保存到数据库,然后下面的GridView
查询数据库把上面删除的添加到下面,反之同理;
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mButton; private MySqlite sqlite; private SQLiteDatabase db; private List<String> list_my; private List<String> list_other; private View inflate; private GridView my_pindao; private GridView other_pindao; private MyAdapter adapter_my; private MyAdapter adapter_other; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //实例化数据库对象 sqlite = new MySqlite(this); db = sqlite.getWritableDatabase(); //我的频道集合 list_my = new ArrayList<>(); list_my.add("热点"); list_my.add("财经"); list_my.add("科技"); list_my.add("段子"); list_my.add("汽车"); list_my.add("时尚"); list_my.add("房产"); list_my.add("彩票"); list_my.add("独家"); //其他频道集合 list_other = new ArrayList<>(); list_other.add("头条"); list_other.add("首页"); list_other.add("娱乐"); list_other.add("图片"); list_other.add("游戏"); list_other.add("体育"); list_other.add("北京"); list_other.add("军事"); list_other.add("历史"); list_other.add("教育"); } private void initView() { mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); } @Override public void onClick(View v) { int a = 0; switch (v.getId()) { case R.id.button: //拿到PopupWindow用到的布局 inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.pindao, null); //获取控件 my_pindao = inflate.findViewById(R.id.my_pindao); other_pindao = inflate.findViewById(R.id.other_pindao); //添加适配器 adapter_my = new MyAdapter(list_my, MainActivity.this); my_pindao.setAdapter(adapter_my); adapter_other = new MyAdapter(list_other, MainActivity.this); other_pindao.setAdapter(adapter_other); //实例化PopupWindow并设置宽高(宽高不设置是出不来的) PopupWindow popupWindow = new PopupWindow(inflate, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); //设置popuwindow外部可以点击 popupWindow.setOutsideTouchable(true);//可以点击 //设置popuwindow里面的listView有焦点(GridView也可以) popupWindow.setFocusable(true); //给popupWindow设置动画 popupWindow.setBackgroundDrawable(new ColorDrawable()); if (a == 0) { //显示在那个控件下面 popupWindow.showAsDropDown(mButton); Log.d("PY", "弹出"); a = 1; } else if (a == 1) { popupWindow.dismiss(); Log.d("PY", "关闭"); a = 0; } //我的频道的条目点击 my_pindao.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.d("PY", "点击了"); //获取点击的条目 String item2 = (String) adapter_my.getItem(i); Log.d("PY", item2 + i); //添加到数据库 ContentValues values = new ContentValues(); values.put("title", item2); db.insert("item", null, values); //把点击的这个条目从集合里删除掉并刷新适配器 list_my.remove(i); Log.d("PY", "删除" + i); adapter_my.notifyDataSetChanged(); //从数据库中查询到删除的这个条目 Cursor cursor = db.query("item", null, null, null, null, null, null); //遍历查询结果 String string = null; while (cursor.moveToNext()) { string = cursor.getString(cursor.getColumnIndex("title")); //把刚才我的频道删除的数据添加到下面其他频道里 list_other.add(string); adapter_other.notifyDataSetChanged(); } //删除这个数据库中的所有数据,(我们需要循环执行点击删除添加,也就是循环查询和添加,不删除的话 // ,查询添加的时候就会重复上面已经添加过的) db.delete("item", null, null); } }); //其他频道条目的点击 other_pindao.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //这里的操作差不多和上面是一样的 //获取点击的条目 String item1 = (String) adapter_other.getItem(i); //添加到数据库 ContentValues values1 = new ContentValues(); values1.put("title", item1); db.insert("item", null, values1); //把点击的这个条目从集合里删除掉并刷新适配器 list_other.remove(i); adapter_other.notifyDataSetChanged(); //从数据库中查询到刚删除的这个条目 Cursor cursor1 = db.query("item", null, null, null, null, null, null); //遍历查询结果 String string1 = null; while (cursor1.moveToNext()) { string1 = cursor1.getString(cursor1.getColumnIndex("title")); //把刚才我的频道删除的数据添加到我的频道里 list_my.add(string1); adapter_my.notifyDataSetChanged(); } //删除这个数据库中的所有数据,(我们需要循环执行点击删除添加,也就是循环查询和添加,不删除的话 // ,查询添加的时候就会重复上面已经添加过的) db.delete("item", null, null); } }); break; } } }
适配器:
public class MyAdapter extends BaseAdapter { private List<String> list; private Context context; public MyAdapter(List<String> list, Context context) { this.list = list; this.context =context; } @Override public int getCount() { Log.d("PY",list.size()+"-----"); return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { viewHolder holder=null; if (view==null){ holder=new viewHolder(); view=View.inflate(context,R.layout.item,null); holder.tv= view.findViewById(R.id.tv); view.setTag(holder); }else{ holder= (viewHolder) view.getTag(); } holder.tv.setText(list.get(i)); return view; } class viewHolder{ TextView tv; } }
数据库:
public class MySqlite extends SQLiteOpenHelper { public MySqlite(Context context) { super(context, "item.db", null, version); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table item(id integer primary key autoincrement,title text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
下面是简单的布局:
activity_main
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="频道管理"/>
popupwindow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:textColor="#f22" android:textSize="35dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我的频道" /> <View android:background="#585858" android:layout_width="match_parent" android:layout_height="2dp" /> <GridView android:numColumns="4" android:id="@+id/my_pindao" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:background="#585858" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout> <View android:layout_marginTop="80dp" android:background="#585858" android:layout_width="match_parent" android:layout_height="2dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:textColor="#f22" android:textSize="35dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="其他频道" /> <View android:background="#585858" android:layout_width="match_parent" android:layout_height="2dp" /> <GridView android:numColumns="4" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/other_pindao" /> </LinearLayout> </LinearLayout>
item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="75dp"
android:layout_height="1dp"
android:background="#123fff"
/>
<TextView
android:background="#fff123"
android:text="啊啊啊啊啊"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:minHeight="38.0dip"
android:minWidth="72.0dip"
android:textSize="14.0sp"
android:id="@+id/tv"/>
<!--minHeight设置文本区域的最小高度-->
<View
android:layout_width="75dp"
android:layout_height="1dp"
android:background="#123fff"
/>
</LinearLayout>
最后实现的效果是: