我已经尝试过使用可点击和可聚焦但似乎没有任何东西可以让列表项可以点击。
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add_recipe_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/recipe_activity_add"
android:background="@color/colorPrimary"
android:textColor="#fff"/>
<ListView
android:id="@+id/recipe_list"
android:layout_width="match_parent"
android:focusable="false"
android:clickable="true"
android:layout_height="match_parent" />
java代码
recipeListView = (ListView)findViewById(R.id.recipe_list);
//make viewRecipe Work
recipeListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("MainActivity", "ListView item clicked.");
}
});
答案 0 :(得分:0)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by Zohaib Hassan on 11/28/2016.
*/
public class InboxAdapter extends ArrayAdapter<InboxRow> {
ArrayList<InboxRow> items;
Context context;
public InboxAdapter(Context context, int resource, ArrayList<InboxRow> items) {
super(context , resource , items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
InboxRow rowItem = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.inbox_row, null);
viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_user_name_inbox);
viewHolder.tvMessage = (TextView) convertView.findViewById(R.id.tv_message_inbox);
viewHolder.tvTimeCount = (TextView) convertView.findViewById(R.id.tv_time_count_inbox);
viewHolder.userProfilePic = (CircleImageView) convertView.findViewById(R.id.inbox_profile_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
/*CircleImageView ivProfileImage Set Background with Picasso*/
return convertView;
}
private static class ViewHolder {
TextView tvUserName , tvMessage , tvTimeCount;
CircleImageView userProfilePic;
}
}
最后一件事,请确保&#34; ListView&#34;在您的布局类中设置为&#34; match-parent&#34;高度和宽度。