在android中有两个listview的活动

时间:2015-06-17 09:19:40

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

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>

我想要的是在一个活动中显示两个活动。 我面临的唯一问题是看起来我无法正确显示布局,因为它只显示带滚动条的第二个布局而不是scrollview的滚动

3 个答案:

答案 0 :(得分:0)

您的问题出在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>

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

<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>