后来才发现需要在布局中设置android:descendantFocusability="blocksDescendants"
在ImageView中设置android:focusable="false"
然后又用到了ViewHolder来提升listview的性能,下边是list_item.xml的代码
参考实现ListView中点击ImageView的点击的博客
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/fruit" />
<LinearLayout
android:id="@+id/buy_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:orientation="vertical" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title_info"
android:textSize="12dp" />
<LinearLayout
android:id="@+id/buy_number_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
android:orientation="horizontal" >
<TextView
android:id="@+id/descTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="150dp"
android:text="Description"
android:textSize="12dp" />
<ImageView
android:id="@+id/imageView_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:focusable="false"
android:src="@drawable/delete" />
<TextView
android:id="@+id/list_buy_number"
android:layout_width="20dp"
android:layout_height="20dp"
android:text="0"
android:textSize="15sp" />
<ImageView
android:id="@+id/imageView_add"
android:layout_width="20dp"
android:layout_height="20dp"
android:focusable="false"
android:src="@drawable/add" />
LinearLayout>
LinearLayout>
RelativeLayout>
public final class ViewHolder {
TextView titleTextView;
TextView descTextView;
ImageView imageView_delete;
TextView list_buy_number;
ImageView imageView_add;
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mListItem.size();
}
@Override
public Object getItem(int position) {
return mListItem.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("InflateParams") @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Map item = new HashMap();
item = mListItem.get(position);//获得所有数据
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
holder.titleTextView = (TextView) convertView
.findViewById(R.id.titleTextView);
holder.descTextView = (TextView) convertView
.findViewById(R.id.descTextView);
holder.imageView_delete = (ImageView) convertView
.findViewById(R.id.imageView_delete);
holder.list_buy_number = (TextView) convertView
.findViewById(R.id.list_buy_number);
holder.imageView_add = (ImageView) convertView
.findViewById(R.id.imageView_add);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleTextView.setText(item.get("titleTextView").toString());
holder.descTextView.setText(item.get("descTextView").toString());
//点击添加和减少商品的图标
holder.imageView_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "delete user", Toast.LENGTH_SHORT).show();
}
});
holder.imageView_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "add user", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
}
我的测试做的比较成功,但是在添加到自己的项目中的时候出现了很多问题,数据的获取和适配器的链接,调了半天的bug,最后在公司老大的指导下,(竟然没有实例化,我哭会。。。)
JSON数据的处理过程中,需要开辟线程从网络获取数据,然后处理数据的时候,需要用到Handler进行处理信息(例如:线程中不能实现UI。。。具体细节,大家多动手,多查查资料。)
private void parseJSONWithJSONObject(String jsonData) {
try {
//String jsonData="[{'id':'5','version':'5.5','name':'Angry Birds'},{'id':'6','version':'7.0','name':'Angry Dog'}]";
//String jsonData="http://www.xxxjqt.cn/amallphone/index.php/Home/Mobile/";
JSONArray jsonArray = new JSONArray(jsonData);
Map<String, Object> map ;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
map = new HashMap<String, Object>();
/*String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");*/
String id = jsonObject.getString("gid");
String name = jsonObject.getString("price");
String version = jsonObject.getString("original_price");
map.put("titleTextView", id);
map.put("descTextView", name);
mListItem.add(map);
Log.d("MainActivity", "id is " + id);
Log.d("MainActivity", "name is " + name);
Log.d("MainActivity", "version is " + version);
}
Toast.makeText(getActivity(), mListItem.size(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://www.xxxjqt.cn/amallphone/index.php/Home/Mobile/");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
//parseJSONWithJSONObject(response);
Message message = new Message();
message.what = 1;
message.obj = response.toString();
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
String response = (String) msg.obj;
parseJSONWithJSONObject(response);
//Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
MyAdapter adapter = new MyAdapter(getActivity());
mListView.setAdapter(adapter);
mListView_1.setAdapter(adapter);
// 设置进入选购中心显示的商品界面,修复进入界面是加载全部的信息问题
mListView.setVisibility(View.INVISIBLE);
mListView_1.setVisibility(View.INVISIBLE);
// getActivity使用在适配器中适用于Fragment中
ArrayAdapter typeAdapter = new ArrayAdapter
(getActivity(), android.R.layout.simple_list_item_1, data);
lv_type.setAdapter(typeAdapter);
// 分类适配器类别添加点击事件
lv_type.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
// 对点击位置进行判断,添加事件
if (data[position].equals("热卖专区")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("进口精品")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.VISIBLE);
}
if (data[position].equals("国产优选")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("森林干果")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("礼盒整箱")) {
mListView.setVisibility(View.GONE);
mListView_1.setVisibility(View.GONE);
}
if (data[position].equals("新鲜果汁")) {
mListView.setVisibility(View.VISIBLE);
mListView_1.setVisibility(View.GONE);
}
}
});
break;
}
}
};