我创建了一个非常简单的应用程序,用户发布了一个提供给主页的更新,这是通过自定义列表适配器和列表的自定义布局完成的。这一切都很好,但我想让listView中的每个项目都可点击,当点击隐藏的评论框下拉以允许用户添加评论时再次点击评论框隐藏但显示有多少评论(类似于Facebook )。无法在线查找与评论框特别相关的内容。我已经阅读了关于ExpandableListView的androidhive教程,这似乎是正确的方法,只是不知道如何为评论框自定义它。我将如何实现这一目标?
ListAdapter:
public class ListAdapter extends ArrayAdapter<ParseObject> {
private Context mContext;
private List<ParseObject> statusObj;
public ListAdapter(Context con, List<ParseObject> statusObj) {
super(con, R.layout.customlistlayout, statusObj);
mContext = con;
this.statusObj = statusObj;
}
@Override //this inflates each row on homepage
public View getView(final int position, View convert, ViewGroup parent) {
ViewHolder holder;
if (convert == null) { //if no view present create one
convert = LayoutInflater.from(mContext).inflate(R.layout.customlistlayout, null);
holder = new ViewHolder();
holder.statusHomePage = (TextView) convert.findViewById(R.id.statusHomePage);
holder.userHomePage = (TextView) convert.findViewById(R.id.userHomePage);
holder.timestampHomePage = (TextView) convert.findViewById(R.id.timestampHomePage);
convert.setTag(holder);
} else {
holder = (ViewHolder) convert.getTag();
}
//gets position of status in table from cloud database
ParseObject status = statusObj.get(position);
//sets username to the one who posted update
String username = status.getString("Username");
holder.userHomePage.setText(username);
//sets status to the content user posted
String userStatus = status.getString("Status");
holder.statusHomePage.setText(userStatus);
//sets the date and time for each post
DateFormat df = DateFormat.getDateTimeInstance();
String timestamp = df.format(status.getCreatedAt());
holder.timestampHomePage.setText(timestamp);
return convert;
}
public static class ViewHolder {
TextView userHomePage;
TextView statusHomePage;
TextView timestampHomePage;
}
主页onCreate:
public class HomePage extends ListActivity {
private List<ParseObject> mStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
getActionBar().setTitle("Update Feed");
ParseQuery<ParseObject> query = new ParseQuery<>("Status");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
mStatus = parseObjects;
ListAdapter list = new ListAdapter(getListView().getContext(), mStatus);
setListAdapter(list);
} else {
Toast.makeText(HomePage.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
HomePage.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
tools:context="main.HomePage">
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>
CustomListLayout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar1"
android:orientation="vertical">
<TextView
android:id="@+id/userHomePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_user"
android:gravity="center_vertical"
android:text="@string/UsernameCustom"
android:textStyle="bold"
android:textSize="16sp"/>
<TextView
android:id="@+id/statusHomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/status"
android:textSize="18sp"/>
<TextView
android:id="@+id/timestampHomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/createdAt"
android:textSize="14sp"/>
答案 0 :(得分:0)
根据评论部分,我会尝试隐藏/显示评论部分,具体取决于视图中的项目是否被触及。
您可以使用.findViewById(R.id.commentLayout)
定位commentLayout,然后继续使用.setVisibility(View.VISIBLE)
和.setVisibility(View.GONE);
您可能最终必须创建一个自定义视图类来专门响应视图的触摸,而不是触摸ListView。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar1"
android:orientation="vertical">
<TextView
android:id="@+id/userHomePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_user"
android:gravity="center_vertical"
android:text="@string/UsernameCustom"
android:textStyle="bold"
android:textSize="16sp"/>
<TextView
android:id="@+id/statusHomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/status"
android:textSize="18sp"/>
<TextView
android:id="@+id/timestampHomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/createdAt"
android:textSize="14sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/commentLayout"
android:orientation="vertical">
<TextView
android:id="@+id/tvComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/createdAt"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
以下是我在应用程序中使用的ParseQueryAdapter示例。
public class SportEventQueryAdapter extends ParseQueryAdapter<SportEvent> {
// modify to show results based on relation. User should only see what
// events he is associated with.
public SportEventQueryAdapter(Context context, final ParseUser currentUser) {
// Use the QueryFactory to construct a PQA that will only show
// Todos marked as high-pri
super(context, new ParseQueryAdapter.QueryFactory<SportEvent>() {
public ParseQuery<SportEvent> create() {
// ParseQuery<SportEvent> query = new ParseQuery<SportEvent>(
// "SportEvent");
ParseRelation relation = currentUser.getRelation("events");
ParseQuery query = relation.getQuery();
//query.whereEqualTo("user", ParseUser.getCurrentUser());
query.orderByDescending("events");
return query;
}
});
}
@Override
public View getItemView(SportEvent event, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.gridview_detail, null);
}
super.getItemView(event, v, parent);
// Add and download the image
ParseImageView todoImage = (ParseImageView) v
.findViewById(R.id.ivMap);
ParseFile imageFile = event.getParseFile("photo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
// Add the title view
TextView typeTextView = (TextView) v.findViewById(R.id.tvSportType);
typeTextView.setText(event.getSport());
TextView titleTextView = (TextView) v.findViewById(R.id.tvTitle);
titleTextView.setText(event.getTitle());
// Add the desc view
TextView dateTextView = (TextView) v.findViewById(R.id.tvDate);
dateTextView.setText(event.getDate());
return v;
}
}
另一个例子
public class SportEventSearchQueryAdapter extends ParseQueryAdapter<SportEvent> {
// modify constructor to take a zipcode, only show results near users
// zipcode
public SportEventSearchQueryAdapter(Context context,
final ParseGeoPoint location, final String sportType, final int miles) {
// Use the QueryFactory to construct a PQA that will only show
// Todos marked as high-pri
super(context, new ParseQueryAdapter.QueryFactory<SportEvent>() {
public ParseQuery<SportEvent> create() {
ParseQuery<SportEvent> query = new ParseQuery<SportEvent>(
"SportEvent");
query.whereEqualTo("sport", sportType);
query.whereNear("location", location);
query.whereWithinMiles("location", location, miles);
query.orderByDescending("createdAt");
return query;
}
});
}
@Override
public View getItemView(SportEvent event, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.gridview_detail, null);
}
super.getItemView(event, v, parent);
// Add and download the image
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.ivMap);
ParseFile imageFile = event.getParseFile("photo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
// Add the title view
TextView typeTextView = (TextView) v.findViewById(R.id.tvSportType);
typeTextView.setText(event.getSport());
TextView titleTextView = (TextView) v.findViewById(R.id.tvTitle);
titleTextView.setText(event.getTitle());
// Add the desc view
TextView dateTextView = (TextView) v.findViewById(R.id.tvDate);
dateTextView.setText(event.getDate());
return v;
}
}
使用解析关系的示例
public class FriendQueryAdapter extends ParseQueryAdapter<ParseUser> {
ImageButton btnAdd;
ImageView ivAdd;
public FriendQueryAdapter(Context context, final ParseUser currentUser) {
// refine query to not show currentUser
super(context, new ParseQueryAdapter.QueryFactory<ParseUser>() {
public ParseQuery<ParseUser> create() {
// ParseQuery<ParseUser> query = ParseUser.getQuery();
ParseRelation relation = currentUser.getRelation("Friends");
ParseQuery query = relation.getQuery();
query.orderByAscending("Friends");
return query;
}
});
}
@Override
public View getItemView(final ParseUser pUser, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.search_detail, null);
}
super.getItemView(pUser, v, parent);
ParseImageView todoImage = (ParseImageView) v
.findViewById(R.id.imageViewSearch);
ParseFile imageFile = pUser.getParseFile("photo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
// Add the title view
final TextView titleTextView = (TextView) v
.findViewById(R.id.textViewSearch);
titleTextView.setText(pUser.getUsername());
final TextView titleTextViewTagline = (TextView) v
.findViewById(R.id.textViewSearchTagline);
titleTextViewTagline.setText(pUser.getString("tagline"));
return v;
}
}