【问题标题】:Scroll Linerlayout with ListView使用 ListView 滚动线性布局
【发布时间】:2014-12-15 13:37:11
【问题描述】:

我即将完成一个 Android 应用程序的开发,但在呈现配置文件片段布局时遇到了困难。 基本上我到目前为止所做的是将用户的信息放在 LinearLayout 的顶部,在底部我有一个列表视图,其中包含他/她迄今为止发布的所有消息。通过这样做,我无法在滚动 ListView 时滚动 LinearLayout。

当有人滚动 ListView(如 Facebook 个人资料页面)时,我想做的是滚动带有所有用户信息的 LinearLayout。

这是现有的 XML:**

<LinearLayout>
    <LinearLayout>
        <!-- User information. -->
    </LinearLayout>

    <ListView/>

</LinearLayout>

【问题讨论】:

  • 您可以将列表视图中的第一项设置为与其余项不同的布局,并用用户信息填充它。请参阅this 了解如何执行此操作。
  • 尝试在ListView头部布局中添加用户信息。

标签: android android-layout listview android-listview


【解决方案1】:

使用自定义列表视图

public class CustomExpandableListView extends ListView {

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

ListView(Context context) {
    super(context);
}

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

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    // Calculate entire height by providing a very large height hint.
    // View.MEASURED_SIZE_MASK represents the largest height possible.
    int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
            MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);

    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
}
}

更换你的

 <ListView/> 

<com.wsc.common_methods.ListView/>

并将您的整体布局放在滚动视图中

【讨论】:

    【解决方案2】:

    定义两个布局用于显示用户信息和消息,一个布局包含ListView,另一个布局包含用户信息视图,在活动中加载ListView布局并将用户信息布局添加到ListView header。

    user_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    
    </LinearLayout>
    

    user_header.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/txtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    用户活动

    private ListView listView;
    private TextView txtUserName;
    private View header;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_main);
    
        listView = (ListView) findViewById(R.id.listView);
        header = LayoutInflater.from(this).inflate(R.layout.user_header,null);
        txtUserName= (TextView) header.findViewById(R.id.txtUserName);
    
        listView.addHeaderView(header);
    }
    

    【讨论】:

    • 这就是我正在搜索的内容。谢谢。
    • @whit3hawks,很高兴为您提供帮助。
    【解决方案3】:

    为此,您必须将孔视图放在 ScrollView 中。您的 Xml 父布局应该是 Scroll View。要在滚动视图中正确滚动列表视图,您必须使用 ExpandableHeight Listview。

    请查看链接1 - enter link description here

    link2 - enter link description here

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      相关资源
      最近更新 更多