1. 程式人生 > >頻道管理簡單實現

頻道管理簡單實現

此網址可以參考
https://blog.csdn.net/min121515/article/details/79439019
1、首先在專案的build下注冊
allprojects {
repositories {
jcenter()
//下面這句才是加上的,上面的自帶 的
maven {url “https://jitpack.io”}
}
}
2、然後在app下的build中匯入依賴
compile ‘com.github.andyoom:draggrid:v1.0.1’

程式碼實現:
public class MainActivity extends AppCompatActivity {
private List muserdata = new ArrayList<>();
private List motherdata = new ArrayList<>();
private GridView user_grid;
private GridView other_grid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initData();
    initView();
    MyGridAdapter adapterUser = new MyGridAdapter(muserdata,MainActivity.this);
    MyGridAdapter adapterOther = new MyGridAdapter(motherdata,MainActivity.this);
    user_grid.setAdapter(adapterUser);
    other_grid.setAdapter(adapterOther);
}

private void initData() {
    muserdata.add("新聞");
    muserdata.add("財經");
    muserdata.add("娛樂");
    muserdata.add("視訊");
    muserdata.add("數碼");
    muserdata.add("汽車");
    muserdata.add("熱點");
    muserdata.add("科技");
    muserdata.add("問答");
    muserdata.add("美女");
    muserdata.add("趣圖");
    muserdata.add("軍事");

    motherdata.add("小說");
    motherdata.add("體育");
    motherdata.add("歷史");
    motherdata.add("手機");
    motherdata.add("育兒");
    motherdata.add("寵物");
    motherdata.add("房產");
    motherdata.add("休閒");
    motherdata.add("養生");
    motherdata.add("情感");
    motherdata.add("傢俱");
    motherdata.add("直播");
}

private void initView() {
    user_grid = (GridView) findViewById(R.id.user_grid);
    other_grid = (GridView) findViewById(R.id.other_grid);
}

}
adapter內容:
public class MyGridAdapter extends BaseAdapter {

private List<String> mdatas;
private Context context;

public MyGridAdapter(List<String> mdatas, Context context) {
    this.mdatas = mdatas;
    this.context = context;
}

@Override
public int getCount() {
    return mdatas.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView==null){
        convertView = View.inflate(context, R.layout.grid_item,null);
        holder = new ViewHolder();
        holder.gtvItem = convertView.findViewById(R.id.tv_grid_item);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.gtvItem.setText(mdatas.get(position));
    return convertView;
}
class ViewHolder{
    TextView gtvItem;
}

}