編輯:關於Android編程
import com.example.exmrecycler.R; import com.example.exmrecycler.interfaces.OnItemClickListener; import com.example.exmrecycler.interfaces.OnItemLongClickListener; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class RecyclerAdapter extends RecyclerView.Adapterimplements OnItemClickListener, OnItemLongClickListener { private final static String TAG = "RecyclerAdapter"; private Context mContext; private LayoutInflater mInflater; private int mType; private String[] mTitleArray; public RecyclerAdapter(Context context, int type, String[] titleArray) { mContext = context; mInflater = LayoutInflater.from(context); mType = type; mTitleArray = titleArray; } @Override public int getItemCount() { return mTitleArray.length; } @Override public ViewHolder onCreateViewHolder(ViewGroup vg, int viewType) { View v = null; ViewHolder holder = null; v = mInflater.inflate(R.layout.list_title, vg, false); holder = new TitleHolder(v); return holder; } @Override public void onBindViewHolder(ViewHolder vh, final int position) { TitleHolder holder = (TitleHolder) vh; holder.tv_seq.setText(""+(position+1)); holder.tv_title.setText(mTitleArray[position]); LayoutParams params = holder.ll_item.getLayoutParams(); if (mType == 1) { //表示是線性布局 params.height = 50; holder.ll_item.setLayoutParams(params); } else if (mType == 2) { //表示是網格布局 params.height = 100; holder.ll_item.setLayoutParams(params); } else { //表示是瀑布流網格布局 params.height = (int) Math.round(300 * Math.random()); if (params.height < 60) { params.height = 60; } //很奇怪,setLayoutParams對瀑布流網格不起作用,只能用setHeight holder.tv_title.setHeight(params.height); } //列表項的點擊事件需要自己實現 holder.ll_item.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { mOnItemClickListener.onItemClick(v, position); } } }); holder.ll_item.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { if (mOnItemLongClickListener != null) { mOnItemLongClickListener.onItemLongClick(v, position); } return true; } }); } @Override public int getItemViewType(int position) { //這裡返回每項的類型,開發者可自定義頭部類型與一般類型, //然後在onCreateViewHolder方法中根據類型加載不同的布局,從而實現帶頭部的網格布局 return 0; } @Override public long getItemId(int position) { return position; } public class TitleHolder extends RecyclerView.ViewHolder { public LinearLayout ll_item; public TextView tv_seq; public TextView tv_title; public TitleHolder(View v) { super(v); ll_item = (LinearLayout) v.findViewById(R.id.ll_item); tv_seq = (TextView) v.findViewById(R.id.tv_seq); tv_title = (TextView) v.findViewById(R.id.tv_title); } } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } private OnItemLongClickListener mOnItemLongClickListener; public void setOnItemLongClickListener(OnItemLongClickListener listener) { this.mOnItemLongClickListener = listener; } @Override public void onItemClick(View view, int position) { String desc = String.format("您點擊了第%d項,內容是%s", position+1, mTitleArray[position]); Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(View view, int position) { String desc = String.format("您長按了第%d項,內容是%s", position+1, mTitleArray[position]); Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show(); } }
import com.example.exmrecycler.adapter.RecyclerAdapter; import com.example.exmrecycler.widget.DividerItemDecoration; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.LinearLayout; public class LinearActivity extends Activity { private RecyclerView rv_linear; private LinearLayoutManager mLayoutManager; private RecyclerAdapter mAdapter; private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"}; private String[] yearArray = {"鼠年", "牛年", "虎年", "兔年", "龍年", "蛇年", "馬年", "羊年", "猴年", "雞年", "狗年", "豬年"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_linear); rv_linear = (RecyclerView) findViewById(R.id.rv_linear); mLayoutManager = new LinearLayoutManager(this); mLayoutManager.setOrientation(LinearLayout.VERTICAL); rv_linear.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(this, 1, yearArray); rv_linear.setAdapter(mAdapter); rv_linear.setItemAnimator(new DefaultItemAnimator()); rv_linear.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); } }
import com.example.exmrecycler.adapter.RecyclerAdapter; import com.example.exmrecycler.widget.DividerGridItemDecoration; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; public class GridActivity extends Activity { private RecyclerView rv_grid; private GridLayoutManager mLayoutManager; private RecyclerAdapter mAdapter; private String[] yearArray = {"鼠年", "牛年", "虎年", "兔年", "龍年", "蛇年", "馬年", "羊年", "猴年", "雞年", "狗年", "豬年"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid); rv_grid = (RecyclerView) findViewById(R.id.rv_grid); mLayoutManager = new GridLayoutManager(this, 4); // mLayoutManager.setSpanCount(3); // //以下占位規則的意思是:第一項占四列,第二列和第三項各占兩列 // //如果網格的列數為四,那麼第一項將占滿第一行,第二列和第三項平分第二行,第三行開始每行有四項 // mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { // @Override // public int getSpanSize(int position) { // if (position == 0) { // return 4; // } else if (position==1 || position==2) { // return 2; // } else { // return 1; // } // } // }); rv_grid.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(this, 2, yearArray); rv_grid.setAdapter(mAdapter); rv_grid.setItemAnimator(new DefaultItemAnimator()); rv_grid.addItemDecoration(new DividerGridItemDecoration(this)); } }
import com.example.exmrecycler.adapter.RecyclerAdapter; import com.example.exmrecycler.widget.SpacesItemDecoration; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.widget.LinearLayout; public class StaggeredActivity extends Activity { private RecyclerView rv_staggered; private StaggeredGridLayoutManager mLayoutManager; private RecyclerAdapter mAdapter; private String[] yearArray = {"鼠年", "牛年", "虎年", "兔年", "龍年", "蛇年", "馬年", "羊年", "猴年", "雞年", "狗年", "豬年"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_staggered); rv_staggered = (RecyclerView) findViewById(R.id.rv_staggered); mLayoutManager = new StaggeredGridLayoutManager(4, LinearLayout.VERTICAL); // mLayoutManager.setSpanCount(3); // mLayoutManager.setOrientation(LinearLayout.HORIZONTAL); // mLayoutManager.setReverseLayout(true); rv_staggered.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(this, 3, yearArray); rv_staggered.setAdapter(mAdapter); rv_staggered.setItemAnimator(new DefaultItemAnimator()); //每項周圍的空隙是5,那麼項與項之間的間隔就是5+5=10。 SpacesItemDecoration decoration=new SpacesItemDecoration(5); rv_staggered.addItemDecoration(decoration); } }
Loader(加載器)簡介Android 3.0 中引入了加載器,支持輕松在 Activity 或Fragment中異步加載數據。 加載器具有以下特征:(1)可用於每個
前段時間公司項目中有用到Bezier曲線的知識,在熟悉Bezier曲線原理和實現方式後,我突發奇想在Android客戶端實現Bezier曲線的構建動畫,於是有了Bezie
有時候,我們的微信會收到一些不認識的人發的廣告,甚至是一些微商的詐騙信息,特別一些微信群上,時不時就會遇到一些做垃圾廣告或者不法分子詐騙的,或者不小心就被加
本文實例講述了Android編程使用ListView實現數據列表顯示的方法。分享給大家供大家參考,具體如下:要將數據庫中的數據列表顯示在屏幕上,我們要使用ListView