【问题标题】:An activity with two listviews in androidandroid 中有两个列表视图的活动
【发布时间】:2015-09-02 10:29:41
【问题描述】:

MainAcitivity

public class MainActivity extends Activity {

ListView list, listYear;
String[] web = {
        "Google Plus",
        "Twitter",
        "Windows",
        "Bing",
        "Itunes",
        "Wordpress",
        "Drupal"
} ;
String[] year = {
        "2012",
        "2013",
        "2014",
        "2015",
        "2016",
        "2017",
        "2018"
} ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list=(ListView)findViewById(R.id.list);
    list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, web));
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
        }
    });

    listYear=(ListView)findViewById(R.id.listYear);
    listYear.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, year));
    listYear.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + year[+position], Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

layout_activity

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="YEAR" />

            <ListView
                android:id="@+id/listYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp"
                android:layout_marginTop="10dip"
                android:background="#B29090" >
            </ListView>

        </LinearLayout>

        <LinearLayout
            android:layout_below="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_marginTop="10dip"
                android:text="WEBSITE" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp"
                android:layout_marginTop="10dip"
                android:background="#B29090" >
            </ListView>
        </LinearLayout>

    </RelativeLayout>

</ScrollView>

我想要的是在一个活动中显示这两个活动。 我面临的唯一问题是,我似乎无法正确显示布局,因为它只显示带有滚动条的第二个布局,而不是滚动视图的滚动

【问题讨论】:

  • 尝试替换它......但仍然没有用
  • 只删除scorllview ...
  • 您知道自定义列表视图..吗?
  • 是的,我知道
  • 1 fill_parent 已弃用,因为 API 级别 8。使用match_parent。 2 为什么这一切可怕 layout nesting?。 3 ListViews inside Scrollviews 是邪恶的!!

标签: android android-layout android-activity layout android-listview


【解决方案1】:

您的问题在于您的RelativeLayout 在您的ScrollView 下。

尝试将其替换为垂直的 LinearLayout 并为其 2 个子 LinearLayout 赋予 1 的权重,如下所示:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dip" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="YEAR" />

            <ListView
                android:id="@+id/listYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#B29090"
                android:dividerHeight="1dp" >
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dip" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="WEBSITE" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#B29090"
                android:dividerHeight="1dp" >
            </ListView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

【讨论】:

  • 它肯定会帮助我,但它没有用
  • '因为我想实现这样的东西camo.githubusercontent.com/…
  • 那么您需要使用 1 个不带 ScrollView 的单个自定义 ListView(因为滚动将与列表视图一起出现)并且逻辑不同于一维字符串数组
  • 但是我将如何创建像我展示给你的图像中的部分
  • 这将取决于您将如何填充您的列表视图
【解决方案2】:

试试这个骨架..可能对你有帮助

<LinearLayout orintion='vertical'>
   <LinearLayout weight='1'>
      <Listview /> <Other controls>
   </LinearLayout>
   <LinearLayout weight='1'>
       <Listview /> <Other controls>
   </LinearLayout>
</LinearLayout>

【讨论】:

【解决方案3】:
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
        <LinearLayout
        android:id="@+id/vertical"
        android:layout_width="0dp"
        android:layout_weight="30"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/Listbyoperator_listvirtacel"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#ffffff"
            android:background="#C92B2B"
            android:dividerHeight="1px">

        </ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/horizontal"

         android:layout_width="0dp"
        android:layout_weight="70"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/Listbyoperator_detaillist"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>

    </LinearLayout>
    </LinearLayout>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
相关资源
最近更新 更多