简体   繁体   中英

vertical list view with horizontal scroll view android

I am trying to implement a list view with horizontal scroll view. I am getting a logcat error "null pointer exception " while setting list view adapter. My list view should be displayed inside sherlock fragment

public class Trending extends SherlockFragment

{
List<String> list;
    ListView prog_list;


// Array of integers points to images stored in /res/drawable-ldpi/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) 
{
          View myFragmentView = inflater.inflate(R.layout.trendslay, container,false);       
          prog_list = (ListView)myFragmentView.findViewById(R.id.lv1);
          return prog_list;
         }


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

    list = new ArrayList<String>();
    list.add("hi");
    list.add("hello");
    list.add("how r u");
    list.add("hi");
    list.add("hello");
    list.add("how r u");
    list.add("hi");
    list.add("hello");
    list.add("how r u");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
              android.R.layout.simple_list_item_1, android.R.id.text1,list);
    prog_list.setAdapter(adapter);


    }

I had the same requirement of creating a Horizontal ListView inside Vertical ListView and I had tried to manage that using TwoWayView for Horizontal scrolling of ListView items with Vertical scrolling. You can check my demo for the same on github as VerticalHorizontalListView.

You can use vertical scroll view

and in your layout give android:fillViewport="true" this will give you horizontal scroll effect without using horizontal scroll view

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    </LinearLayout>
</ScrollView>

Add prog_list.setHorizontalScrollBarEnabled(true); after setAdapter();

TRY THIS:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) 
{
      View myFragmentView = inflater.inflate(R.layout.trendslay,container,false);       
      prog_list = (ListView)myFragmentView.findViewById(R.id.lv1);


    list = new ArrayList<String>();
    list.add("hi");
    list.add("hello");
    list.add("how r u");
    list.add("hi");
    list.add("hello");
    list.add("how r u");
    list.add("hi");
    list.add("hello");
    list.add("how r u");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>getActivity(),
          android.R.layout.simple_list_item_1, android.R.id.text1,list);
    prog_list.setAdapter(adapter);

    return  myFragmentView ;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM