gpt4 book ai didi

java - android 中 ListView 内的 ListView

转载 作者:行者123 更新时间:2023-12-02 03:27:01 27 4
gpt4 key购买 nike

我在 ListView 中使用 ListView ,并且两个 ListView 共享相同的列表项。它工作正常,但如果项目的高度在内部 ListView 中增加,它不会包装内容并放置不符合要求的 ScrollView 。请参阅下面的代码

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">

<ListView
android:id="@+id/list_comments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:divider="@android:color/transparent"
android:dividerHeight="5dp" />

</RelativeLayout>

comments_for_topic.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#fafafa">

<RelativeLayout
android:id="@+id/commentsRelLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="7dp"
android:background="#d5d5d5"
android:orientation="vertical">
<TextView
android:id="@+id/UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"

android:text="User name"/>
<TextView
android:id="@+id/Comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/UserName"
android:textSize="12sp"
android:padding="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/showRep"
android:text="Comment test 1"/>
<TextView
android:id="@+id/showRep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2Replies"
android:layout_toLeftOf="@+id/Reply"
android:layout_marginRight="3dp"
android:textSize="12dp"
android:textColor="@android:color/holo_blue_dark"
android:layout_below="@+id/UserName"/>

<TextView
android:id="@+id/Reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply"
android:textSize="12dp"
android:textColor="@android:color/holo_blue_dark"
android:layout_below="@+id/UserName"
android:layout_alignParentRight="true"/>
</RelativeLayout>


<ListView
android:id="@+id/list_commentsLevel1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/commentsRelLayout"
android:layout_alignLeft="@+id/commentsRelLayout"
android:layout_marginLeft="34dp"
android:background="#d5d5d5"
android:layout_marginTop="2dp"
android:scrollbars="none"
android:divider="@android:color/white"
android:dividerHeight="1dp"
></ListView>
<TextView
android:id="@+id/showmorebtn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_more"
android:layout_below="@+id/list_commentsLevel1"
android:layout_alignLeft="@+id/list_commentsLevel1"
android:textColor="@android:color/holo_blue_dark"
/>

</RelativeLayout>

AdapterComments.java

public class AdapterComments extends BaseAdapter {

private Activity activity;
private ArrayList<CommentListModel> itemsArrayList = new ArrayList<CommentListModel>();
private ArrayList<CommentListModel> Comments = new ArrayList<CommentListModel>();
CommentListModel tempValues=null;
private static LayoutInflater inflater=null;
ArrayList<AdapterComments> adapterCommentsArrayList = new ArrayList<AdapterComments>();
ViewHolder holder = new ViewHolder();
ViewHolder viewHolder;
ArrayList<String> list =new ArrayList<String>();
private int currentID=0;
boolean isChild =false;
FragmentDiscussinTopic fragmentDiscussinTopic;

public AdapterComments() {
}

public AdapterComments(Activity context, FragmentDiscussinTopic fragmentDiscussinTopic, ArrayList<CommentListModel> itemsArrayList) {
this.activity = context;
this.itemsArrayList = itemsArrayList;
this.isChild = false;
this.fragmentDiscussinTopic = fragmentDiscussinTopic;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0 ; i<itemsArrayList.size();i++){
AdapterComments adapterComments1 = new AdapterComments();
adapterCommentsArrayList.add(adapterComments1);
}

}

public AdapterComments(Activity context, ArrayList<CommentListModel> itemsArrayList) {
this.activity = context;
this.itemsArrayList = itemsArrayList;
this.isChild = true;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0 ; i<itemsArrayList.size();i++){
AdapterComments adapterComments1 = new AdapterComments();
adapterCommentsArrayList.add(adapterComments1);
}

}

@Override
public int getCount() {
if(itemsArrayList.size()<=0)
return 1;
return itemsArrayList.size();
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public Object getItem(int position) {
return position;
}



public static class ViewHolder{
public TextView UserName;
public TextView Comment;
public TextView showRep;
public TextView Reply;
public TextView showmorebtn;
public ListView list_commentsLevel1;
}

@Override
public View getView(final int position, View rowView, ViewGroup parent) {
View v1 = rowView;





if(rowView==null){

v1 = inflater.inflate(R.layout.comments_for_topic,null);


viewHolder =new ViewHolder();
viewHolder.UserName=(TextView)v1.findViewById(R.id.UserName);
viewHolder.Comment=(TextView)v1.findViewById(R.id.Comment);
viewHolder.showRep=(TextView)v1.findViewById(R.id.showRep);
viewHolder.showmorebtn=(TextView)v1.findViewById(R.id.showmorebtn);
viewHolder.Reply=(TextView)v1.findViewById(R.id.Reply);
viewHolder.list_commentsLevel1=(ListView)v1.findViewById(R.id.list_commentsLevel1);
v1.setTag( viewHolder );
}
else
viewHolder=(ViewHolder)v1.getTag();

if(itemsArrayList.size()<=0)
{
Log.e("data", " no data found");

}
else {


tempValues = itemsArrayList.get(position);

if (tempValues.isVisible()){
viewHolder.list_commentsLevel1.setAdapter(adapterCommentsArrayList.get(position));

viewHolder.list_commentsLevel1.setVisibility(View.VISIBLE);
ObjectAnimator anim = ObjectAnimator.ofFloat(viewHolder.list_commentsLevel1, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();
setListViewHeightBasedOnChildren(viewHolder.list_commentsLevel1);
// scrollMyListViewToBottom();

}else {
Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
viewHolder.list_commentsLevel1.startAnimation(animation);
viewHolder.list_commentsLevel1.setVisibility(View.GONE);
}

viewHolder.UserName.setText(tempValues.getUserName());
viewHolder.Comment.setText(tempValues.getComment());
int subcmntcount = tempValues.getSubCommentCount();
if (subcmntcount>=1){

viewHolder.showRep.setVisibility(View.VISIBLE);
if (subcmntcount==1){
String repText=new String("1Reply");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.showRep.setText(content);
}else {
String repText=new String(subcmntcount+"Replies");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.showRep.setText(content);
}
viewHolder.showmorebtn.setVisibility(View.GONE);
}else {
viewHolder.showRep.setVisibility(View.GONE);
viewHolder.list_commentsLevel1.setVisibility(View.GONE);
viewHolder.showmorebtn.setVisibility(View.GONE);
}

if (tempValues.isChild()){
viewHolder.Reply.setVisibility(View.GONE);
viewHolder.showRep.setVisibility(View.GONE);
}else {
viewHolder.Reply.setVisibility(View.VISIBLE);
String repText=new String("Reply");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.Reply.setText(content);
}



v1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {



for (int i = 0;i<itemsArrayList.size();i++){
CommentListModel commentListModel = ( CommentListModel ) itemsArrayList.get( i );
if (i==position){

isChild =true;
if (commentListModel.getSubCommentCount()>0){
commentListModel.setVisible(true);
itemsArrayList.remove(i);
itemsArrayList.add(i,commentListModel);
new getDiscussionForumComments(commentListModel.getDiscussionCommentID(),position).execute();
}


}else {
commentListModel.setVisible(false);
itemsArrayList.remove(i);
itemsArrayList.add(i,commentListModel);
}
}

}
});


viewHolder.Reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Configuration.currentDiscussID = itemsArrayList.get(position).getDiscussionCommentID();
fragmentDiscussinTopic.requestEditPop();
}
});
}
return v1;
}
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;

int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;

for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);

if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
ViewGroup.LayoutParams.WRAP_CONTENT));

view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();

}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

listView.setLayoutParams(params);
listView.requestLayout();

}
}

请注 Intent 像 ListView 的突出显示区域高度不适合包裹内容 please see the highlighted area of image listview height is not proper to wrap the content

如有任何帮助,我们将不胜感激

最佳答案

问题是你无法同时获取两个 ListView 的滚动。如果你同时做的话。您需要创建一个自定义 ListView 尝试类似的方法而不是您的 ListView (内部列表)。

  public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}

public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}

关于java - android 中 ListView 内的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com