RecyclerView
android中有一个组件叫做RecyclerView,用它可以实现横向滑动的ListView列表,还可以实现瀑布流等多种炫酷的功能。
使用RecyclerView的流程是:创建一个RecyclerView界面,然后再创建一个RecycleViewAdapter适配器,创建一个布局管理器,就可以运用RecyclerView了。
public class DoFragment extends Fragment {
private RecyclerView recyclerView;
private RecycleViewAdapter adapter;
private List<User> listData;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.do_fragment, null);
recyclerView = (android.support.v7.widget.RecyclerView) view
.findViewById(R.id.recyclerView);
LinearLayoutManager layout = new LinearLayoutManager(getActivity());
layout.setOrientation(LinearLayoutManager.HORIZONTAL);
listData = new ArrayList<User>();
adapter = new RecycleViewAdapter(listData, getActivity());
recyclerView.setLayoutManager(layout);
recyclerView.setAdapter(adapter);
httpData();
return view;
}
int page = 0;
int count = 10;
private void httpData() {
String urlRoot = "http://192.168.1.104:8080/MyTextWeb/";
String url = urlRoot + "MsgServlet";
RequestParams params = new RequestParams();
params.add("page", page + "");
params.add("count", count + "");
HttpUtil.post(url, params, new CommonHttpHandler() {
@Override
public void success(int arg0, Header[] arg1, byte[] bs)
{
String result = new String(bs);
// json 解析
try
{
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject userMsg = jsonArray.getJSONObject(i);
// userMsg.get
User user = new User();
user.headImg = userMsg.getString("headImg");
user.name = userMsg.getString("name");
listData.add(user);
}
adapter.setListData(listData);
} catch (JSONException e)
{
e.printStackTrace();
}
}
});
}
}
public class RecycleViewAdapter extends Adapter<MyViewHolder>{
List<User> listData;
Context context;
public RecycleViewAdapter(List<User> listData, Context context)
{
this.listData = listData;
this.context = context;
}
@Override
public int getItemCount() {
if (listData == null)
return 0;
return listData.size();
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int position) {
//界面赋值
User user = listData.get(position);
Picasso.with(context).load(user.headImg).into(myViewHolder.mIvHead);
myViewHolder.mTvName.setText(user.name);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
//加载布局
View view = View.inflate(context, R.layout.msg_list_item, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
class MyViewHolder extends ViewHolder{
public SmartImageView mIvHead;
public TextView mTvName;
public MyViewHolder(View view) {
super(view);
mIvHead = (SmartImageView) view.findViewById(R.id.ivHead);
mTvName = (TextView) view.findViewById(R.id.tvName);
}
}
public void setListData(List<User> listData)
{
this.listData = listData;
notifyDataSetChanged();
}
}
然后效果图就是:
DrawerLayout
drawerLayout是一个旁拉框组件,类似QQ的个人设置,其设置的过程是:在布局文件里面设置布局,然后通过java关联配置文件,设置数据,再添加监听就可以了。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rgMsg"
style="@style/mainRbStyle"
android:drawableTop="@drawable/main_btn1_selector"
android:text="消息" />
<RadioButton
android:id="@+id/rgContact"
style="@style/mainRbStyle"
android:drawableTop="@drawable/main_btn2selector"
android:text="联系人" />
<RadioButton
android:id="@+id/rgDo"
style="@style/mainRbStyle"
android:drawableTop="@drawable/main_btn3selector"
android:text="动态" />
</RadioGroup>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="300dp"
android:layout_gravity="start"
android:background="#c7edcc"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.DrawerLayout>
drawerLayout = (android.support.v4.widget.DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.listView);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("key", "item1");
data.add(map);
map = new HashMap<String, String>();
map.put("key", "item2");
data.add(map);
map = new HashMap<String, String>();
map.put("key", "item3");
data.add(map);
String[] from = { "key" };
int[] to = { R.id.tvName };
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
R.layout.item, from, to);
listView.setAdapter(simpleAdapter);
// 长点击
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
CommonApplication.makeText("长点击" + position);
return true;
}
});
// 点击
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
CommonApplication.makeText("点击" + position);
}
});
结果图示: