2012-12-04 129 views
4

我想在ListView中製作一個水平滾動視圖。我有ListView的工作,但把數據放入horizo​​ntalscrollview(HSV)不適合我。請指教! (我編碼的列表視圖,測試,現在試圖添加horizo​​ntalscrollview)ListView內的Horizo​​ntalScrollView

HSV將爲每個listitem。所以基本上我的邏輯是如何接近這個:我有我的列表視圖適配器,我決定把HSV放在適配器內,這樣它通過每個listItem循環,並在其中放置一個HSV。

我的XML lookis這樣的:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:baselineAligned="false" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="0dip" android:layout_weight="1" 
    android:layout_height="fill_parent"> 


     <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtProjectName" /> 

     <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtProjectDescription" /> 

     <HorizontalScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" > 

      <LinearLayout 
       android:id="@+id/projectTasks" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtProjectTasks" /> 

      </LinearLayout> 

     </HorizontalScrollView> 
    </LinearLayout> 
</LinearLayout> 

然後創建一個自定義適配器通過所有項目運行。

public class ProjectListAdapter extends ArrayAdapter<Projects> { 

int resource; 
String response; 
Context context; 
ArrayList<Tasks> taskArray = null; 

// Initialize adapter 
public ProjectListAdapter(Context context, int resource, 
     List<Projects> items) { 
    super(context, resource, items); 
    this.resource = resource; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LinearLayout projectView; 

    // Get the current project object 
    Projects project = getItem(position); 
    // 
    // Inflate the view 
    if (convertView == null) { 
     projectView = new LinearLayout(getContext()); 
     String inflater = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater vi; 
     vi = (LayoutInflater) getContext().getSystemService(inflater); 
     vi.inflate(resource, projectView, true); 
    } else { 
     projectView = (LinearLayout) convertView; 
    } 

    TextView PROJECT_NAME = (TextView) projectView 
      .findViewById(R.id.txtProjectName); 
    TextView PROJECT_DESCRIPTION = (TextView) projectView 
      .findViewById(R.id.txtProjectDescription); 

    PROJECT_NAME.setText(project.getNAME()); 
    PROJECT_DESCRIPTION.setText(project.getDESCRIPTION()); 
    taskArray = new ArrayList<Tasks>(); 
    taskArray = (ArrayList<Tasks>) project.getTasks(); 

    for (Tasks tasks : taskArray) { 
     HorizontalScrollView TASKS = (HorizontalScrollView) projectView 
       .findViewById(R.id.projectTasks); 
     LinearLayout taskLayout = (LinearLayout) projectView 
       .findViewById(R.id.projectTasks); 
     TextView taskTxt = (TextView) projectView 
       .findViewById(R.id.txtProjectTasks); 
     taskTxt.setText(tasks.getTASK_ID()); 
     taskLayout.addView(taskTxt); 

    } 
    return projectView; 
} 

}

這代碼最後一位是其中適配器將遍歷使HSV的事,但沒有工作,我怎麼想,請請幫助!

+0

檢查此鏈接http://stackoverflow.com/a/4492050/1705641。 – Evos

回答

1

請按照下面的鏈接有HSV ListView中: http://www.dev-smart.com/archives/34

OR

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    boolean handled = mGesture.onTouchEvent(event); 
    return handled; 
} 
Then, add the following code which will decide to steal the event from the item children and give it to our onTouchEvent, or let it be handled by them. 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    switch(ev.getActionMasked()){ 
     case MotionEvent.ACTION_DOWN: 
      mInitialX = ev.getX(); 
      mInitialY = ev.getY();    
      return false; 
     case MotionEvent.ACTION_MOVE: 
      float deltaX = Math.abs(ev.getX() - mInitialX); 
      float deltaY = Math.abs(ev.getY() - mInitialY); 
      return (deltaX > 5 || deltaY > 5); 
     default: 
      return super.onInterceptTouchEvent(ev); 
    } 
} 
Finally, don't forget to declare the variables in your class: 

private float mInitialX; 
private float mInitialY; 

來源:Horizontal ListView in Android?

+0

嘿謝謝,生病了。我正在嘗試製作horizo​​ntalscrollviews列表。這是可能的嗎? – deep

+0

你的意思是說每個項目在ListView作爲單純皰疹病毒?我不認爲它可能與上述代碼。嘗試上面的代碼,讓我知道你面對什麼問題... – Shrikant

+0

是的,這正是我想要做的。檢出Android的脈搏新聞應用程序。這與我試圖達到的相似。 – deep

0

在你的代碼這行也不行,你要創建一個新的linearLayout但它不是來自您的自定義視圖:

projectView = new LinearLayout(getContext()); 

那麼試試這個:

View projectView = convertView; 

// Inflate the view 
if (projectView == null) { 
    String inflater = Context.LAYOUT_INFLATER_SERVICE; 
    LayoutInflater vi; 
    vi = (LayoutInflater) getContext().getSystemService(inflater); 
    projectView = vi.inflate(resource, false); 
} 

,並定義projectView作爲視圖,而不是一個LinearLayout中。

+0

我做了這些改變,並測試了沒有HSV和代碼的工作,但HSV的東西不起作用。我相信我沒有正確地誇大它。你能建議嗎? – deep

+0

好吧,如果你做了我所說的話,可能它不是在膨脹......但我可能是你的listView獲得觸摸事件,所以你的HSV無法得到它。您可能需要處理觸摸事件。 – yahya

+0

我的HSV在它的佈局中只有一個TextView,但是當HSV被填充後,它將具有每個HSV的多個TextView。 – deep

相關問題