android - 如何使自定义 ListView 像附加图像一样

标签 android listview android-custom-view

我正在使用 RSS 提要。我使用带适配器的 ListView 在包含“带图像和文本的 LinearLayout”的自定义 View 中显示数据。此 View 在每一行中重复出现。但是,我需要制作如下图所示的自定义 View ,以使用不同的 View 显示数据。

customView

最佳答案

步骤:

  1. 标签栏可以用水平 ScrollView 完成,里面有按钮。单击按钮更改该按钮的宽度和高度。并重置最后选择的一个。

  2. 彩色水平线可以是另一种线性布局。您在单击按钮时更改其颜色。

  3. 您可以在同一个 ListView 中使用不同的布局。一件买两件,一件买一件。

  4. 在适配器 getCount 方法中,您应该计算您拥有的行数。基于用于决定哪些行可以有一项以及哪些行可以有两项的算法。

那个 ListView 并不像你想象的那么复杂。

工作示例:

enter image description here

I created a github repository for it.

适配器类:

public class ArticlesAdapter extends ArrayAdapter<Article> {

    int mod;

    public ArticlesAdapter(Context context, int resource, ArrayList<Article> teams) {
        super(context, resource, teams);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        int resource = R.layout.single_item;
        if (position % 2 == 0)
            resource = R.layout.double_item;
        if (position == getCount()-1 && mod == 1)
            resource = R.layout.single_item;

        if (view == null || view.getTag() != resource)
        {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(resource, null);
            view.setTag(resource);
        }

        TextView tv1 = (TextView)view.findViewById(R.id.textView1);
        ImageView iv1 = (ImageView)view.findViewById(R.id.imageView1);

        int index1 = (position * 3 / 2) + (position * 3) % 2;
        Article article1 = getItem(index1);

        tv1.setText(article1.getDesc());
        iv1.setImageResource(article1.getImageUrl());

        if (resource == R.layout.double_item)
        {
            TextView tv2 = (TextView)view.findViewById(R.id.textView2);
            ImageView iv2 = (ImageView)view.findViewById(R.id.imageView2);

            int index2 = index1 + 1;
            Article article2 = getItem(index2);

            tv2.setText(article2.getDesc());
            iv2.setImageResource(article2.getImageUrl());
        }

        return view;
    }

    @Override
    public int getCount() {
        int count = super.getCount();
        mod = count % 3;
        count = count / 3 * 2;
        return count + (mod > 0 ? 1 : 0);
    }
}

主要 Activity 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="@dimen/tabbar_height"
        android:background="@android:color/black"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="bottom"
            android:baselineAligned="false">

            <Button
                android:id="@+id/button1"
                android:layout_width="@dimen/button_width"
                android:layout_height="match_parent"
                android:background="@color/red"
                android:text="@string/top_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="0"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/orange"
                android:text="@string/entertain_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="1"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button3"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/green"
                android:text="@string/world_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="2"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button4"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/blue"
                android:text="@string/biz_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="3"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button5"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/purple"
                android:text="@string/sport_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="4"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button6"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/pink"
                android:text="@string/games_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="5"
                android:onClick="tabClicked"/>

            <Button
                android:id="@+id/button7"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_height"
                android:background="@color/yellow"
                android:text="@string/tech_title"
                android:textColor="@color/white"
                android:textSize="@dimen/tabbar_text_size"
                android:textAllCaps="false"
                android:tag="6"
                android:onClick="tabClicked"/>

        </LinearLayout>

    </HorizontalScrollView>

    <LinearLayout
        android:id="@+id/tabbarLineLL"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="@color/red"
        android:orientation="vertical">

    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp">

    </ListView>

</LinearLayout>

选项卡点击方法:

public void tabClicked(View view)
{
    LinearLayout.LayoutParams tempLL;

    // reset current selected button size
    Button currentButton = tabs[selected_index];
    tempLL = (LinearLayout.LayoutParams)currentButton.getLayoutParams();
    tempLL.width = (int) getResources().getDimension(R.dimen.button_width);
    tempLL.height = (int) getResources().getDimension(R.dimen.button_height);
    currentButton.setLayoutParams(tempLL);

    // change selected tab
    selected_index = Integer.parseInt(view.getTag().toString());

    // change color for the new selected button
    tempLL = (LinearLayout.LayoutParams)view.getLayoutParams();
    tempLL.width = (int) getResources().getDimension(R.dimen.button_width);
    tempLL.height = (int) getResources().getDimension(R.dimen.tabbar_height);
    view.setLayoutParams(tempLL);

    // change tabbar line color
    ColorDrawable buttonColor = (ColorDrawable) view.getBackground();
    tabbarLineLL.setBackgroundColor(buttonColor.getColor());

    setupAdapter();
}

public void setupAdapter()
{
    // setup adapter for selected tab
    articlesAdapter = new ArticlesAdapter(this, 0, articles.get(selected_index));
    listview.setAdapter(articlesAdapter);
    articlesAdapter.notifyDataSetChanged();
}

关于android - 如何使自定义 ListView 像附加图像一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31777340/

相关文章:

android - 如何获取谷歌项目 ID 和 pushwoosh 应用程序 ID?

android - 如何在用户打开下一个 fragment 时停止 CountDownTimer 计数?

android - 从 Parcelable ArrayList 重新填充列表

android - 光标在 KitKat 上的空自定义 EditText 中不可见

Android Media Player 停止后无法播放

java - 从文件中获取 VideoUri 内容时出现 CursorIndexOutOfBoundsException

java - 将对象添加到 JSONObj

android - 使用基本适配器和 ListView 显示来自 SQLite 的变量数据

java - 在自定义 View 中嵌入 RecyclerView

android - 设置 android 后自定义 View 为空白 : targetSdkVersion ="14" or above