频道管理+popuwindow

1.然后在app下的build中导入依赖
 compile 'com.github.andyoom:draggrid:v1.0.1'

2.main_activity布局
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwei.com.pdgl.MainActivity">

    <Button
        android:id="@+id/btn"
        android:text="频道管理"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

3.Main_Activity类
public class MainActivity extends AppCompatActivity {
    Button btn;
    List<String> list_my;
    GridView viewById;
    GridView viewById1;
    List<String> list_other;
    View inflate;
    private MysqLite sqlite;
    private SQLiteDatabase db;
    MyAdapter adapter;
    MyAdapter adapter_other;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        btn = (Button) findViewById(R.id.btn);
        //实例化数据库对象
        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("教育");
        //按钮点击事件
      btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int a =0;
                switch (view.getId()){
                    case R.id.btn:
                        //拿到popu用到的布局
                         inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.popu, null);
                       viewById = (GridView) inflate.findViewById(R.id.my_pindao);
                         viewById1 = (GridView) inflate.findViewById(R.id.other_pindao);
                        //添加适配器
                      adapter = new MyAdapter( MainActivity.this,list_my);
                        viewById.setAdapter(adapter);
                      adapter_other = new MyAdapter( MainActivity.this,list_other);
                        viewById1.setAdapter(adapter_other);

                        //实例化PopupWindow并设置宽高(宽高不设置是出不来的)
                        //参数一:是popu布局  参数二:宽度  参数三:高度
                        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(btn);
                            Log.d("PY", "弹出");
                            a = 1;
                        } else if (a == 1) {
                            popupWindow.dismiss();
                            Log.d("PY", "关闭");
                            a = 0;
                        }
            //条目点击事件
       viewById.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //获取点击条目
                        String item2 = (String) adapter.getItem(i);
                        //添加到数据库
                        ContentValues values = new ContentValues();
                        values.put("title", item2);
                        db.insert("item", null, values);
                        //把点击的这个条目从集合里删除掉并刷新适配器
                        list_my.remove(i);
                        adapter.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);
                    }
                });
        //条目点击事件
 viewById1.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.notifyDataSetChanged(); } //删除这个数据库中的所有数据,(我们需要循环执行点击删除添加,也就是循环查询和添加,不删除的话 // ,查询添加的时候就会重复上面已经添加过的) db.delete("item", null, null); } }); break; } } }); }}
4.适配器item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <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>
5.适配器
public class MyAdapter extends BaseAdapter{
    List<String>list;
    Context context;
    //有参构造方法
    public MyAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }
        
    @Override         

//返回listview显示的总条数

 public int getCount() { return list.size(); } @Override   //返回当前位置的数据对象  public Object getItem(int i) { return list.get(i); } @Override ////返回当前视图的子视图的id号 一般 返回传入的int 值  public long getItemId(int i) { return i; } @Override //返回当前位置的子视图对象,需要我们在这里进行子视图的绘制 public View getView(int i, View view, ViewGroup viewGroup) { viewHolder holder=null; //判断当前view是否为空  if (view==null){ holder=new viewHolder();

// 该方法是用来加载item显示的布局的 一定要从layout资源中拿

 view=View.inflate(context,R.layout.item,null); holder.tv= (TextView) view.findViewById(R.id.tv);

// 把一个对象 放入到View中

// 执行该代码的目的 是让曾经创建过的控件 和 已经存在的View 进行了绑定

 view.setTag(holder); }else{

 //因为在else的逻辑中 convertView是被复用的 既然是被复用的 肯定曾经和一个TextView进行过绑定

//所以这事 TextView就不需要从新创建 从convertView取出来即可

  holder= (viewHolder) view.getTag(); } holder.tv.setText(list.get(i)); return view; }

// google文档推荐 ViewHolder使用静态类 如果不用静态 也可以起到优化的作用

 class viewHolder{ TextView tv; } } 6.频道popu布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <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>
7.数据库
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) {

    }
}



 

 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值