2016-12-10 2 views
1

ich eine Listenansicht der Suche Produkte haben aus verschiedenen Web-Seiten geholt wie so:mit einem Element Interagieren in Android Listview wirkt auf mehrere

Visual Aid

Es gibt eine horizontale Scroll-Ansicht auf den Titel, so dass Benutzer Sehen Sie sich den Titel an. Wenn ich jedoch einen der Einträge scrolle, wird ein weiterer Punkt um 6 von dem Objekt, mit dem ich gerade interagiert habe, ebenfalls nach rechts gescrollt. Wie kann ich das verhindern?

Update: Hier ist mein CustomAdapter Code:

public class CustomAdapter extends ArrayAdapter<Item> implements View.OnClickListener{ 

private ArrayList<Item> dataSet; 
Context mContext; 

// View lookup cache 
private static class ViewHolder { 
    TextView itemTitle; 
    TextView itemStore; 
    TextView itemPrice; 
    ImageView storeLogo; 
} 

public CustomAdapter(ArrayList<Item> data, Context context) { 
    super(context, R.layout.row_item, data); 
    this.dataSet = data; 
    this.mContext=context; 

} 

@Override 
public void onClick(View v) { 

    int position=(Integer) v.getTag(); 
    Object object= getItem(position); 
    Item item=(Item)object; 

    switch (v.getId()) 
    { 
     case R.id.store: 
      Snackbar.make(v, "This item is being sold at " + item.getStore() + "." , Snackbar.LENGTH_LONG) 
        .setAction("No action", null).show(); 
      break; 
    } 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    Item item = getItem(position); 

    // Check if an existing view is being reused, otherwise inflate the view 
    ViewHolder viewHolder; // view lookup cache stored in tag 

    final View result; 

    if (convertView == null) { 

     viewHolder = new ViewHolder(); 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(R.layout.row_item, parent, false); 
     viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title); 
     //viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type); 
     viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price); 
     viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store); 

     result = convertView; 

     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
     result = convertView; 
    } 

    viewHolder.itemTitle.setText(item.getTitle()); 
    viewHolder.itemPrice.setText("$" + item.getPrice()); 
    viewHolder.storeLogo.setOnClickListener(this); 
    viewHolder.storeLogo.setTag(position); 

    // Return the completed view to render on screen 
    return convertView; 

} 

}

Hier ist meine fragment_search.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.painlessshopping.mohamed.findit.Search$PlaceholderFragment"> 



<Button 
    android:text="Next Page" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonUp" 
    android:onClick="buttonClicked" 
    android:elevation="0dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginStart="49dp" /> 

<Button 
    android:text="Previous Page" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonDown" 
    android:onClick="buttonClicked" 
    android:elevation="0dp" 
    android:layout_marginStart="62dp" 
    android:layout_alignParentBottom="true" 
    android:layout_toEndOf="@+id/buttonUp" /> 

<EditText 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:ems="10" 
    android:id="@+id/editText" 
    android:layout_width="250dp" 
    android:hint="What can I help you find?" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<ListView 
    android:layout_width="match_parent" 
    android:id ="@+id/listView" 
    android:layout_centerVertical="true" 
    android:layout_alignParentStart="true" 
    android:layout_height="375dp" /> 

<Button 
    android:text="Search" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/searchBtn" 
    android:layout_alignParentTop="true" 
    android:layout_toEndOf="@+id/editText" /> 

Hier mein row_item.xml ist (für die benutzerdefinierte Adapter)

<?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="wrap_content" 
android:orientation="vertical" 
android:padding="10dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView" 
    android:layout_width="0px" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:maxLines="1" 
     android:text="Sample Text for Android Horizontal Scroll View Goes Here" 
     android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
     android:textStyle="bold" 
     android:textSize="18sp" 
     android:textColor="@color/dgrey"/> 

</HorizontalScrollView> 


<ImageView 
    android:id="@+id/store" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:src="@drawable/ic_favorite_border_black_24dp" /> 

    </LinearLayout> 

<TextView 
    android:id="@+id/price" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/horizontalScrollView" 
    android:text="$39.99" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
    android:textSize="20sp" 
    android:textColor="@color/colorAccent" 
    android:textStyle="bold" 
    /> 


</LinearLayout> 
+1

Es wäre nützlich sein, um zumindest Ihr Adapter Code zu sehen, oder aber Sie übernehmen die Scroll-Teil, der die anderen wirkt sich –

+0

Ich habe den Adapter-Code und die zwei relevanten Layout-Ressourcen xmls hinzugefügt, hoffentlich hilft das. –

+0

Sind die Ansichten immer gleich weit voneinander entfernt? –

Antwort

1

wegen der Leistung im Konstruktor überprüfen wir und sehen, ob wir in der Lage sind, werden wir das vorherige Layout, das generiert wurde, verwenden und nur versuchen, ihre Werte zu ersetzen, aber in Ihrem Fall, weil Sie die Bildlaufleiste nicht ändern können So sehen Sie, dass Ihr neues Element und die vorherigen Elemente dieselbe Bildlaufleiste anzeigen. Ich hoffe ich war klar. so muss man immer neue Layout wie folgt erzeugen:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// Get the data item for this position 
Item item = getItem(position); 

// Check if an existing view is being reused, otherwise inflate the view 
ViewHolder viewHolder; // view lookup cache stored in tag 

final View result; 


    viewHolder = new ViewHolder(); 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    convertView = inflater.inflate(R.layout.row_item, parent, false); 
    viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title); 
    //viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type); 
    viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price); 
    viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store); 

    result = convertView; 

    convertView.setTag(viewHolder); 

viewHolder.itemTitle.setText(item.getTitle()); 
viewHolder.itemPrice.setText("$" + item.getPrice()); 
viewHolder.storeLogo.setOnClickListener(this); 
viewHolder.storeLogo.setTag(position); 

// Return the completed view to render on screen 
return convertView; 

} 

auch vielleicht es einen besseren Weg gab, die ich weiß nicht, aber das wird das Problem zumindest lösen.

UPDATE: für weitere Informationen darüber, wie Listview Arbeit und Leistung Spitze this link scheinen gute

Verwandte Themen