垂直列表视图与水平滚动视图android

时间:2013-01-24 05:49:12

标签: android

我正在尝试使用水平滚动视图实现列表视图。我在设置列表视图适配器时收到logcat错误“空指针异常”。我的列表视图应该显示在sherlock片段

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


    }

4 个答案:

答案 0 :(得分:6)

我有同样的要求在Vertical ListView中创建一个Horizo​​ntal ListView,我试图使用TwoWayView来管理使用垂直滚动的ListView项目的水平滚动。您可以在github上查看我的演示文稿与VerticalHorizontalListView.

相同

答案 1 :(得分:1)

您可以使用垂直滚动视图

并在您的布局中给出android:fillViewport="true"这将为您提供水平滚动效果,而不使用水平滚动视图

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

答案 2 :(得分:0)

prog_list.setHorizontalScrollBarEnabled(true);

之后添加setAdapter();

答案 3 :(得分:0)

尝试:

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