开发者

Infinite Listview in an HorizonzalScrollView in Android

I am developing an Android app and one of its use cases is to display the following situation.

  1. I have a list of links (L). Each of these links is the URL for a picture in the Internet;
  2. I have to download each开发者_C百科 picture of (L) and display it in a ListView. There should be two rows in the ListView(s), where I can insert the pictures. I want to do something similar to this app;
  3. I have to display the pictures in a HorizontalScrollView;
  4. The pictures have to be downloaded on demand, in other words, I just download the picture using a Thread when the HorizontalScrollView is in a position that shows this picture (similar to this situation).

My questions:

  1. Is it possible to insert an ListView in a HorizontalScrollView? (If yes, how do I do it?)
  2. How do I use HorizontalScrollView? I mean, is there any difference on how I use a ListView inside a ScrollView?

  3. Do you know any plugin/project that has the same purposes?


Question 1 - Perhaps you should re-think your design to use a list of HorizonzalListView.
Question 2 - You can created a list of HorizontialListView programmatically, place them inside a LinearLayout wrapped by a vertical scroll view.

Your myhlist.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

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

</ScrollView>

You activity:

public class ListOfHListlActivity extends Activity {

/** List of ArrayAdapter with each bind to a HorizontialListView created programmatically */
private List<MyAdapter> myAdapters = new ArrayList<MyAdapter>();
/** List of your data model */
private List<Object> myDataList;

/**
 * Worker thread running in background doing dirty job.
 */
private class DoDirtyJobAsyncTask extends AsyncTask<Void, MyAdapter, Void> {

    @Override
    protected Void doInBackground(Void... params) {
    // do your dirty job here, to populate myDataList
        for (Object myData : myDataList) {
            MyAdapter myAdapter = new MyAdapter(myData);
            myAdapters.add(myAdapter);
            publishProgress(myAdapter);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(MyAdapter... myAdapters) {
        int currViewId = 1;
        for (final MyAdapter myAdapter: myAdapters) {
            HorizontialListView listview = new HorizontialListView(getApplicationContext(), null);
            listview.setId(currViewId);
            listview.setAdapter(myAdapter);
            listview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // I am clickable.
                }
            });

            RelativeLayout listOfHListLayout = (RelativeLayout) findViewById(R.id.list_of_hlist_placeholder);
            // don't forget set height here, you know the height issue in HorizontialListView
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT, 40);
            listOfHListLayout.addView(listview, layoutParams);
            currViewId++;
        }
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new DoDirtyJobAsyncTask().execute();
    setContentView(R.layout.myhlist);
}

}


You might be interested in this project http://www.dev-smart.com/archives/34 It talks about implementing the Horizontal ListView in Android without the center locking of the Gallery widget


May be its too late but now android has RecylerView which is way more efficient than Listview and also supports horizontal scroll.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜