代码记录在博客上,分享给想我一样的菜鸟们,自己忘了的时候也可以拿来看看,免得到处找代码:
Fragment中ListView的代码
package com.example.administrator.myapplication14.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.myapplication14.Adapter.MyCommendAdapter;
import com.example.administrator.myapplication14.R;
import com.example.administrator.myapplication14.be.MessageCommend;
import java.util.ArrayList;
import java.util.List;
/**
* @author BULUSILI
* @描述 消息--评论
* @项目名称 宠物圈
* @包名 com.example.administrator.myapplication14.be
* @未知 2016-8-9 09:20
*/
public class CommentFragment extends Fragment {
public ListView mListView;
View v;
MyCommendAdapter mAdapter;
List mList;
//new的实例时候一定要写在onCreateView里面,不然效果会不一样哦
Intent mIntent;
public static CommentFragment newInstance() {
CommentFragment fragment = new CommentFragment();
return fragment;
}
public CommentFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_comment, null);
initView();
initData();
initAdapter();
return v;
}
private void initView() {
mListView = (ListView) v.findViewById(R.id.message_commend_lv);
}
//添加bean类数据临时的先添加,再连数据库
private void initData() {
mList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
MessageCommend message = new MessageCommend("http://img0.imgtn.bdimg.com/it/u=1717816186,2947076118&fm=21&gp=0.jpg",
"小强" + i, "你好", "23:23");
mList.add(message);
//item的点击事件,里面可以设置跳转并传值
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "第" + i + "行", Toast.LENGTH_LONG).show();
}
});
}
}
private void initAdapter() {
//传两个参数过去 1、上下文 2、集合
mAdapter = new MyCommendAdapter(getActivity(), mList);
mListView.setAdapter(mAdapter);
}
}
适配器代码,我的Item内部有个头像,点击头像要跳转到其他页面。点击事件和跳转传值就写在适配器里面:
package com.example.administrator.myapplication14.Adapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.myapplication14.Activity.Main2Activity;
import com.example.administrator.myapplication14.R;
import com.example.administrator.myapplication14.be.MessageCommend;
import java.util.List;
/**
* @author BULUSILI
* @描述 消息评论适配器
* @项目名称
* @包名 com.example.administrator.myapplication14.Adapter
* @2016/8/9--14:52
*/
public class MyCommendAdapter extends BaseAdapter {
List mList;
Context mContext;
public MyCommendAdapter(Context context, List list) {
mList = list;
mContext = context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
ViewHolder mHolder;
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
mHolder = new ViewHolder();
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.commend_state, null);
mHolder.mImageView = (ImageView) view.findViewById(R.id.commend_item_tou_xiang);
mHolder.tv1 = (TextView) view.findViewById(R.id.commend_item_name);
mHolder.tv2 = (TextView) view.findViewById(R.id.commend_item_content);
mHolder.tv3 = (TextView) view.findViewById(R.id.commend_item_time);
view.setTag(mHolder);
} else {
mHolder = (ViewHolder) view.getTag();
}
//如果没有这些内容,将会显示布局文件中的内容
mHolder.mImageView.setImageResource(R.mipmap.ic_launcher);
mHolder.tv1.setText(mList.get(position).getName());
mHolder.tv2.setText(mList.get(position).getText());
mHolder.tv3.setText(mList.get(position).getTime());
//头像的点击事件并传值
mHolder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//开始传值
Intent intent=new Intent(mContext, Main2Activity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("key",mList.get(position));
intent.putExtras(bundle);
//利用上下文开启跳转
mContext.startActivity(intent);
}
});
return view;
}
class ViewHolder {
ImageView mImageView;
TextView tv1;
TextView tv2;
TextView tv3;
}
}
布局文件如下只显示下Itemde 布局了:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageView
android:id="@+id/commend_item_tou_xiang"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/messagescenter_at"/>
RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:orientation="vertical">
<TextView
android:id="@+id/commend_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小凡"
android:textSize="18sp"/>
<TextView
android:id="@+id/commend_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小凡你好小凡你好小凡你好小凡你好小凡你好小凡你好"
android:singleLine="true"
android:textSize="14sp"/>
LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<TextView
android:id="@+id/commend_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2016-12"/>
RelativeLayout>
LinearLayout>
在跳转到的界面接收值:
TextView textView= (TextView) findViewById(R.id.textview333);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
if (bundle!=null){
mMessageCommend =
(MessageCommend) bundle.getSerializable("key");
}
textView.setText(mMessageCommend.getName());