废话不多说首先我们要创建一个xml的主类布局,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="zhangxuelei1506d.myapplication.MainActivity">
<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="我的频道"
/>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<GridView
android:numColumns="4"
android:id="@+id/my_pindao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></GridView>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
</LinearLayout>
<TextView
android:layout_marginTop="80dp"
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<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="其他频道"
/>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<GridView
android:numColumns="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/other_pindao"
></GridView>
</LinearLayout>
</LinearLayout>
其次时创建它的应item的布局 代码走起
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_subscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="38.0dip"
android:minWidth="72.0dip" >
<TextView
android:id="@+id/text_item"
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:layout_margin="5dip" -->
</RelativeLayout>
然后说一下这个频道管理的简单逻辑,首先它是由两个GridView组成, 在GridView中写入一些text的文本点击框。 然后是先每点击一个text他就会对应的消失之后会出现在
对应的一个gridview中。实现这个代码呢 ,需要一个数据库来储存数据然后点击让条目消失刷新适配器,再从数据库取出数据加入到另一个gridview刷新就可以实现效果,接下来开始上码。
主类:
private GridView gridView_my;
private GridView gridView_other;
private SQLiteDatabase db;
private myBaseAdapter adapter_other;
private myBaseAdapter adapter_my;
private List<String> list_my;
private List<String> list_other;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
mySqliteDataBase database = new mySqliteDataBase(this);
db = database.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("教育");
adapter_other = new myBaseAdapter(list_other, this);
gridView_other.setAdapter(adapter_other);
adapter_my = new myBaseAdapter(list_my, MainActivity.this);
gridView_my.setAdapter(adapter_my);
gridView_my.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = (String) adapter_my.getItem(i);
ContentValues values = new ContentValues();
values.put("title",item);
db.insert("item",null,values);
list_my.remove(i);
adapter_my.notifyDataSetChanged();
Cursor cursor = db.query("item", null, null, null, null, null, null);
String str=null;
while (cursor.moveToNext()){
str= cursor.getString(cursor.getColumnIndex("title"));
}
list_other.add(str);
adapter_other.notifyDataSetChanged();
db.delete("item",null,null);
}
});
gridView_other.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = (String) adapter_other.getItem(i);
ContentValues values=new ContentValues();
values.put("title",item);
db.insert("item",null,values);
list_other.remove(i);
adapter_other.notifyDataSetChanged();
Cursor cursor = db.query("item", null, null, null, null, null, null);
String str=null;
while (cursor.moveToNext()){
str= cursor.getString(cursor.getColumnIndex("title"));
}
list_my.add(str);
adapter_my.notifyDataSetChanged();
db.delete("item",null,null);
}
});
}
private void init() {
gridView_my = (GridView) findViewById(R.id.my_pindao);
gridView_other = (GridView) findViewById(R.id.other_pindao);
}
数据库类
public class mySqliteDataBase extends SQLiteOpenHelper{
public mySqliteDataBase(Context context) {
super(context, "item.db", null, 1);
}
@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) {
}
}
适配器类
public class myBaseAdapter extends BaseAdapter{
private List<String> list;
private Context context;
public myBaseAdapter(List<String> list, Context context) {
this.list = list;
this.context=context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder=null;
if (convertView==null){
holder=new viewHolder();
convertView=convertView.inflate(context,R.layout.adapter_mygridview_item,null);
holder.tvContent= (TextView) convertView.findViewById(R.id.text_item);
convertView.setTag(holder);
}else{
holder= (viewHolder) convertView.getTag();
}
holder.tvContent.setText(list.get(position));
return convertView;
}
class viewHolder{
TextView tvContent;
}
}
Successful