2

我目前正在使用 android 开发一个基本的新闻聚合应用程序,我知道那里有很多负载,但我基本上只是创建一个,因为我认为对于刚开始使用 Android 开发的人来说,这是一个很好的起点。

我的目标是显示来自多个提要的文章。每个提要都有自己的水平滑动列表,有点像脉冲新闻应用程序。到目前为止,我已经设法找到有关创建“Horizo​​ntalListView”的教程,现在我的应用程序显示了一个基本的水平滑动列表视图,如下所示:

截图链接: http: //www.dev-smart.com/wp-content/uploads/2011/03/device-200x300.png

脉冲应用程序屏幕截图: http://a1525.phobos.apple.com/us/r1000/080/Purple/v4/ba/6a/01/ba6a01d1-f0b7-4bb7-3f94-5a5761653e3c/mzl.zxnjzzmk.480x480-75。 jpg

代码:

public class HorizontalListViewDemo extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listviewdemo);

    HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
    listview.setAdapter(mAdapter);


}

private static String[] dataObjects = new String[]{ "Text #1",
    "Text #2",
    "Text #3", "Text #4"  }; 

private BaseAdapter mAdapter = new BaseAdapter() {

    private OnClickListener mOnButtonClicked = new OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
            builder.setMessage("hello from " + v);
            builder.setPositiveButton("Cool", null);
            builder.show(); 
        }
    };

    @Override
    public int getCount() {
        return dataObjects.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        Button button = (Button) retval.findViewById(R.id.clickbutton);
        button.setOnClickListener(mOnButtonClicked);
        title.setText(dataObjects[position]);
        return retval;
    }   
};
}

XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#000"
  >

  <com.devsmart.android.ui.HorizontalListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:background="#ddd"
  />

</LinearLayout>
</ScrollView>

正如我所提到的,每个 Horizo​​ntalListView 将代表 1 个提要并如上所述显示其相关文章。因此,我在此开发中的下一步是尝试动态创建这些 Horizo​​ntalListView,以便用户可以从应用程序中声明一个提要,然后在已定义的提要下方动态创建它。

忽略该过程中涉及的所有其他努力,我实际上只是寻求帮助创建一个新的 Horizo​​ntalListView,它可以回收已经定义的 XML listview 布局 id="listview"。我知道如果我在哪里预定义所有提要并创建具有唯一 id 的唯一 XML 布局,我可以让它工作,但我想知道有没有一种方法可以定义一个新的 Horizo​​ntalListView 重用现有布局。

我希望你们能对我有所了解。

谢谢

更新 1: 使用视图?

//Defining main XML file "listviewdemo.xml"
setContentView(R.layout.listviewdemo);
//Creating a new view to apply to my HorizontalListViews, /res/layout/listviewStyle.xml
View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);

//Defining my first HorizontalListView      
HorizontalListView listview = (HorizontalListView) view.findViewById(R.id.listviewReuse);
listview.setAdapter(mAdapter);
4

1 回答 1

1
HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);

This line will always return the first view found with R.id.listview. Not very nice when you have multiple list views.

Now instead of calling Activity.findViewById() call findViewById() on a subview. You can contain each listview inside of a layout. Then call findViewById to find the layout that is the parent to the listview. The call findViewById on the layout.

Once you have found the listView the first time remember it for later use.

In the updated code you posted, remove this line. It is not doing anything b/c this view is not apart of the layout R.layout.listviewdemo.

View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);

Then in The xml for R.layout.listviewdemo add both listviews to the same .xml (assuming that is your goal). Whatever the case maybe you should add all of your views to the same layout then retrieve the views using findViewById.

于 2013-01-27T23:40:09.923 回答