3

リストビューをインスタグラムと同じにしたい...(セクション化)

Googleで検索していくつかの例を見つけましたが、必要に応じて適切に機能していません。これは私が検索したリンクです。リンク 1リンク 2です。しかし、最初のリンクでは、instagram のようなリストビューの解決策が見つかりませんでした。

2 番目のリンクについては、デモを作成して実行したところ、完全に動作しました。唯一の問題は、ヘッダーに 2 つのテキストビューを配置すると、機能しないことです。

これに関して私を助けることができれば、それは私にとって素晴らしいことです..

Second Link からのこのデモ..

package com.example.Section_Listview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class InstaHeaderActivity extends Activity implements AbsListView.OnScrollListener{

    ListView list; 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new Adapter(this));        
        list.setOnScrollListener(this); 
    }

    public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) 
    {     
        //the listview has only few children (of course according to the height of each child) who are visible
        for(int i=0; i < list.getChildCount(); i++)
        {
            View child = list.getChildAt(i);
            ViewHolder holder = (ViewHolder) child.getTag();

            //if the view is the first item at the top we will do some processing
            if(i == 0)
            {             
                boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
                int offset = holder.previousTop - child.getTop();
                if(!(isAtBottom && offset > 0))
                {                    
                    holder.previousTop = child.getTop();
                    holder.header.offsetTopAndBottom(offset);                   
                    holder.header.invalidate();
                }
            } //if the view is not the first item it "may" need some correction because of view re-use
            else if (holder.header.getTop() != 0)
            {
                int offset = -1 * holder.header.getTop(); 
                holder.header.offsetTopAndBottom(offset);
                holder.previousTop = 0;
                holder.header.invalidate();
            }
        }
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {}

    private static class Adapter extends ArrayAdapter<String> {
        public Adapter(Context context) {
            super(context, R.layout.row, R.id.header);
            for(int i=0; i < 50; i++){
                add(Integer.toString(i));
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            if(convertView == null)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
                ViewHolder holder=new ViewHolder();
                holder.header = (TextView) convertView.findViewById(R.id.header);
                convertView.setTag(holder);             
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.header.setText(getItem(position));
            return convertView;
        }
    }



    public static class ViewHolder
    {
        TextView header;
        int previousTop = 0;
    }
}

main.xml

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

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
</LinearLayout>

child_header.xml

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

    <TextView
        android:id="@+id/childHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="childTest" />

</LinearLayout>

行.xml

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

    <ListView
        android:id="@+id/childList"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:layout_marginTop="16dp" >
    </ListView>


    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:padding="12dp"
        android:text="Deneme Row"
        android:textColor="#ffffff" 
        android:background="#000000"/>

</RelativeLayout>
4

1 に答える 1

3

まず、1 つのファイルに 2 つのパブリック クラスを含めることはできないため、移動します。

public class ViewHolder {
    TextView header;
    int previousTop = 0;
}

別のファイルに。その間、 static修飾子を削除してください。許可されていません。

ヘッダー行に 2 つ目の TextView を表示するには、row.xmlで宣言する必要があります。

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

    <ListView
        android:id="@+id/childList"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:layout_marginTop="16dp" >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:background="#000000"
            android:padding="12dp"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/header2"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="#000000"
            android:padding="12dp"
            android:text="second textview"
            android:textColor="#ffffff" />
    </LinearLayout>
</RelativeLayout>

この変更されたrow.xml により、次のようなレイアウトが得られます。 ここに画像の説明を入力

2 番目のテキストビューのコンテンツを設定するには、ViewHolder クラスを次のように変更します。

public class ViewHolder {
    TextView header;
    TextView header2;
    int previousTop = 0;
}

アクティビティの getView-method を次のように変更します。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
            ViewHolder holder = new ViewHolder();
            holder.header = (TextView) convertView.findViewById(R.id.header);
            holder.header2 = (TextView) convertView.findViewById(R.id.header2);
            convertView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.header.setText(getItem(position));
        holder.header2.setText("whatever you want");
        return convertView;
    }
于 2013-08-15T13:28:50.837 に答える