2013-08-12 3 views
3

listview를 instagram과 동일하게 만들고 싶습니다 ... (섹션 화 된)안드로이드에서 Instagram Listview를 만드는 방법?

Google에서 검색하고 예제를 찾았지만 필요에 따라 제대로 작동하지 않습니다. 이것은 내가 검색 한 링크입니다 : Link 1Link 2. 그러나 첫 번째 링크에서 나는 instagram-like listview를위한 해결책을 찾지 못했다.

두 번째 링크는 데모를 실행하고 실행하면 완벽하게 작동합니다. 유일한 문제는 헤더에 두 개의 텍스트 뷰를 넣을 때 작동하지 않는다는 것입니다.

당신에 관한 나를 도울 수 있다면이 그때는 나를 위해 좋은 것입니다 ..

두 번째 링크에서이 데모 ..

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

<?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> 
모든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: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> 

답변

3

첫째, 당신은 하나 개의 파일이 개 공개 수업을하므로 별도의 파일에

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

을 이동할 수 없습니다. 그리고 당신이 그것에있는 동안 고정 수정자를 제거하십시오, 허용되지 않습니다. 이 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에 선언해야 enter image description here

는 두 번째 텍스트 뷰의 내용을 설정하기위한 당신의 ViewHolder 클래스를 변경하려면 :

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

하고 활동에의 getView-방법을 변경 :

@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; 
    } 
+0

두 번째 텍스트 뷰가 표시되지만 나는 내가 다음 문제를 두 번째 텍스트 뷰의 텍스트를 설정하고있는 경우이 code..and를 사용하여 값의 변경할 수 없습니다 3 행 또는 4 행 후에 헤더 자체가 숨겨집니다. –

+0

첫 번째 textview의 내용을 변경하는 것과 같은 방법으로이 작업을 수행 할 수 있습니다. ((TextView) convertView.findViewById (R.id.header)) setText "desired text"); ' –

+0

내 대답에 필요한 코드를 추가했습니다. –

관련 문제