正在削减多个列表视图

时间:2011-10-20 08:50:29

标签: android listview layout android-linearlayout

当我尝试使用多个列表视图进行线性布局时,我遇到了问题。最初当我添加多个包含textview和listview的自定义线性布局时,我无法向下滚动页面,所以我发现添加一个scrollview会解决这个问题,但是由于某些原因,它会裁剪出linearlayout视图,所以我只能看到每个清单的顶部。

以下是不同部分的代码:

主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:weightSum="1">
    <TextView 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:id="@+id/whatsNewTitle" 
        android:text="Whats New?" 
        android:layout_width="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:textSize="30dip">
    </TextView>

    <Button android:layout_height="wrap_content" 
        android:id="@+id/home" 
        android:text="Home" 
        android:layout_weight="0.07" 
        android:layout_width="126dp">
    </Button>
    <ScrollView
        android:id="@+id/sv1"
        android:layout_width="fill_parent" 
        android:fillViewport="true" 
        android:layout_height="630dp"> 
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:id="@+id/whats_new_content" 
            android:layout_height="fill_parent">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

布局im inflating并多次添加到主布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="1">
    <TextView
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/new_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:textSize="25dip">
    </TextView>

    <ListView
        android:id="@+id/new_files"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:background="#000000">
    </ListView>
</LinearLayout>

创建多个listviews并显示它们的主要活动:

public class WhatsNew extends Activity{
private Button homeBtn;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whats_new_main);

        CreateLastModDateList();

        homeBtn = (Button) findViewById(R.id.home);
        homeBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v){
                Intent myIntent = new Intent(getApplication().getApplicationContext(),
                    Main.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }

    private ArrayList<File> GenerateNewFileDates(){
        Files.clearFilesList();
        Files.buildFilesList(Files.getRoot());

        return Files.getAllFiles();
    }

    private void CreateLastModDateList(){
        ArrayList<File> files = GenerateNewFileDates();
        ArrayList<String> allDates = new ArrayList<String>();
        ArrayList<String> dates = new ArrayList<String>();

        Context c = this.getApplicationContext();
        LinearLayout l = (LinearLayout) findViewById(R.id.whats_new_content);

        /** Builds a list of all file dates **/
        for(File file : files){
            Date lastModifiedDate = new Date(file.lastModified());
            allDates.add(lastModifiedDate.toString());
        }

        /** For each date, check if we have already put that date into the dates array
        if we have then we want to ignore it, otherwise add it to the dates array. **/
        for(String date : allDates){
            if (exists(dates,date) == false){
                dates.add(date);
            }
        }

        for(String date : dates){
            ArrayList<String> test = new ArrayList<String>();
            test = generateFileList(date, files);

            WhatsNewLayout whatsNew = new WhatsNewLayout(c,test, date);
            l.addView(whatsNew);
        }
    }

    public ArrayList<String> generateFileList(String date, ArrayList<File> mFiles){
        ArrayList<String> filesFromDate = new ArrayList<String>();

        for(File file : mFiles){
            Date lastModifiedDate = new Date(file.lastModified());
            boolean x = lastModifiedDate.toString().equals(date);

            if (x == true){
                filesFromDate.add(file.getName());
            }
        }
        return filesFromDate;
    }

    public boolean exists(ArrayList<String> list, String compare){
        boolean result = false;
        if(list.contains(compare)){
            result = true;
        }
        return result;
    }
}

自定义线性布局我膨胀:

public class WhatsNewLayout extends LinearLayout  {

    private LayoutInflater mInflater;

    public WhatsNewLayout(Context context, ArrayList<String> files, String date) {
        super(context);
        mInflater = LayoutInflater.from(context);
        View convertView = new View(context);

        /** initialize variables **/
        DataHolder holder;
        Context c = this.getContext();

        ArrayAdapter<String> list = new ArrayAdapter<String>(c,
            android.R.layout.simple_list_item_1, files);

        convertView = mInflater.inflate(R.layout.whats_new_item, null);
        holder = new DataHolder();
        holder.modDate = (TextView) convertView.findViewById(R.id.new_date);
        holder.FileList = (ListView) convertView.findViewById(R.id.new_files);
        convertView.setTag(holder);

        holder.modDate.setText(date);
        holder.FileList.setAdapter(list);

        this.addView(convertView);
    }
}

对于大量的代码感到抱歉,但我不想留下任何细节,以防它重要,希望这是一切,我确定它的东西真的很有趣我的布局调用,但我只是看不出是什么等待自定义布局被削减。

如果需要更多详细信息,请告诉我们。 =)

2 个答案:

答案 0 :(得分:0)

在ScrollView中不可能有一个ListView(因为ListView也是一个可滚动的组件),这就是你的ListView被裁剪到屏幕的原因。

如果您需要在屏幕上显示多个列表以便用户选择某些内容,您可以使用Spinner代替列表

答案 1 :(得分:0)

将listViews放在垂直滚动中。您可以通过以下技巧在垂直滚动内部使用可滚动的listView。使用以下代码享受!

    private int listViewTouchAction;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...

        setListViewScrollable(myListView1);
        setListViewScrollable(myListView2);
    }

    private void setListViewScrollable(final ListView list) {
            list.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    listViewTouchAction = event.getAction();
                    if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                    {
                        list.scrollBy(0, 1);
                    }
                    return false;
                }
            });
            list.setOnScrollListener(new OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view,
                        int scrollState) {
                }

                @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                {
                    list.scrollBy(0, -1);
                }
            }
        });
    }

listViewTouchAction是一个全局整数值。