0

im having an issue when i try to make a linearlayout with multiple listviews. originally when i added multiple custom linearlayouts containing a textview and a listview i could not scroll down the page so i found out adding a scrollview would fix this, which it did but for some reason it then cropped the linearlayout view so i could only see the top part of each list.

heres the code for the different parts:

Main layout

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

layout im inflating and adding multiple times into main layout:

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

the main activity that creates the multiple listviews and displays them:

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

The custom linearlayout i inflate:

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);
    }
}

Sorry about the large mass of code, but i didnt want to leave any details out in case it is important, hopefully this is everything, im sure its something really fundermental with my call of the layout but i just cannot see what is casuing the custom layout to be cut.

if any more details are required please let me know. =)

Deep.Timon
  • 221
  • 5
  • 20

2 Answers2

0

It is not possible to have a ListView inside ScrollView (since the ListView is also a scrollable component) this is why your ListView is cropped to the screen.

If you need to display several List on screen to let user select something, you may use Spinner instead of List

blurab
  • 1
  • thanks for the information. ive managed to get it working though, somehow. don't know why it was cropping still but i went into the custom linearlayout class and manually set the height in layoutParams and passed that to the this.addView and it works fine. again a horrible way of programming but its just for a prototype. thanks again. – Deep.Timon Oct 20 '11 at 09:59
0

Put your listViews in a vertical scroll. You can have scrollable listView inside of a vertical scroll by the following trick. use the following code and enjoy!

    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 is a global integer value.

Bob
  • 22,810
  • 38
  • 143
  • 225
  • 1
    thanks for the solution. ive managed to get it working though, somehow. don't know why it was cropping still but i went into the custom linearlayout class and manually set the height in layoutParams and passed that to the this.addView and it works fine. again a horrible way of programming but its just for a prototype. i havent tried to get the on click listener yet so this could again go horrible bad which means i will probably have to come back and use the code you have given, thanks again as it will really help =) – Deep.Timon Oct 20 '11 at 09:59