2012-10-09 7 views
2

Je dois avoir une marque de textview et d'image comme montré dans l'image. where the textview along with the imageview will be moving Lorsque vous cliquez sur le texte ou Imageview, il devrait ouvrir une activité. Quelqu'un peut-il suggérer un moyen d'y arriver?Marquee de TextView avec ImageView

ImageView doit être cliquable.

Répondre

1

Créé vue personnalisée

public class MarqueeLayout extends ViewGroup { 

private static final int VERTICAL_SPACING = 10; 
private static final int HORIZONTAL_SPACING = 10; 
private static final String TAG = MarqueeLayout.class.getName(); 
private int line_width; 
private List<View> views; 
private Timer timer; 

private int scrollX = 0; 

public MarqueeLayout(Context context) 
{ 
    super(context); 
} 
private Handler handler; 
private int index = 0; 
private int childCount; 
public MarqueeLayout(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    handler = new Handler(); 
    timer = new Timer(); 
    timer.schedule(new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        requestLayout(); 
       } 
      }); 
     } 
    }, 1000, 200); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED); 
    if(views == null) { 
     views = new ArrayList<View>(); 
     childCount = getChildCount(); 
     for(int i = 0; i < childCount; i++) { 
      views.add(getChildAt(i)); 
     } 

    } 
    final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); 
    int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); 
    final int count = getChildCount(); 
    int line_height = 0; 

    int xpos = getPaddingLeft(); 
    int ypos = getPaddingTop(); 

    int childHeightMeasureSpec; 
    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) 
    { 
     childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); 
    } 
    else 
    { 
     childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
    } 

    for (int i = 0; i < count; i++) 
    { 
     final View child = getChildAt(i); 
     if (child.getVisibility() != GONE) 
     { 
      child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec); 
      final int childw = child.getMeasuredWidth(); 
      line_height = Math.max(line_height, child.getMeasuredHeight() + VERTICAL_SPACING); 

      xpos += childw + HORIZONTAL_SPACING; 
     } 
    } 
    this.line_width = xpos; 

    setMeasuredDimension(width, height); 
} 

@Override 
protected ViewGroup.LayoutParams generateDefaultLayoutParams() 
{ 
    return new LayoutParams(1, 1); 
} 

@Override 
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) 
{ 
    if (p instanceof LayoutParams) 
    { 
     return true; 
    } 
    return false; 
} 

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) 
{ 
    Log.d(TAG, "onLayout called"); 
    int count = getChildCount(); 
    final int width = r - l; 

    scrollX -= 20; 
    if(line_width + scrollX < 0) { 
     scrollX = 0; 
    } 
    int i = 0; 
    while(count > 0) { 
     View c = getChildAt(i); 
     if(c == null) { 
      break; 
     } 
     int w = c.getMeasuredWidth(); 
     Log.d(TAG, "scrollX : " + scrollX + " width : " + w); 
     if(scrollX < -w) { 
      this.removeViewAt(0); 
      scrollX += w; 
     } else { 
      break; 
     } 
     i++; 
     count--; 
    } 
    count = getChildCount(); 
    int xpos = getPaddingLeft() + scrollX; 
    int ypos = getPaddingTop(); 
    for (i = 0; i < count; i++) 
    { 
     final View child = getChildAt(i); 
     if (child.getVisibility() != GONE) 
     { 
      final int childw = child.getMeasuredWidth(); 
      final int childh = child.getMeasuredHeight(); 
      child.layout(xpos, ypos, xpos + childw, ypos + childh); 
      xpos += childw + HORIZONTAL_SPACING; 
     } 
    } 
    while(xpos < getWidth()) { 
     View v = views.get(index % childCount); 
     addView(v); 
     xpos += v.getMeasuredWidth(); 
     index++; 
    } 
}} 

et vous pouvez avoir tout type de vue à l'intérieur de cette mise en page, vous pouvez appeler cette mise en page en XML

<Your.Package.name.MarqueeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="this is textview 1" 
     tools:context=".MainActivity" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="clicked" 
     android:text="Click me 1" /> 

</Your.Package.name.MarqueeLayout> 
3

J'atteint marquee sur l'image vue à l'intérieur HorizontalView, similaire que vous décrivez dans votre question, en utilisant CountDownTimer,

Pour votre question, vous devez prendre texview et Imagview dans l'aménagement intérieur HorizontalView et appliquer chapiteau sur HorizontalView ,

check-out code #

public class MyCounter extends CountDownTimer { 

    public MyCounter(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
     if (leftToRight == true) { 

      MCObject.cancel(); 
      //if (gallery.getScrollX() > (((imgv.length - 1) * multiplyValue)+addValue)) { 

      if (gallery.getScrollX() > ((imgv.length - 1) * multiplyValue)+addValue) { 
       count=((imgv.length - 1) *imageHeightWidth); 
       leftToRight = false; 
       MCObject.start(); 
      } 

      if (count != gallery.getScrollX()) { 
       gallery.scrollBy(1, 0); 

       count++; 
       MCObject.start(); 
      } else { 
       count=((imgv.length - 1) * imageHeightWidth); 
       leftToRight = false; 
       MCObject.start(); 
      } 
     } else { 
      MCObject.cancel(); 
      if (gallery.getScrollX() <= 0) { 
       count = -20; 
       leftToRight = true; 
       MCObject = new MyCounter(50, 1); 
       MCObject.start(); 
      } 

      if (count != 0) { 
       gallery.scrollBy(-1, 0); 

       // Log.d("test", ""+hsv.getScrollX()); 
       count--; 
       MCObject.start(); 
      } else { 

       count = -20; 
       leftToRight = true; 
       MCObject = new MyCounter(50, 1); 
       MCObject.start(); 

      } 
     } 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 

    } 

} 

Il donnera marquee de gauche à droite jusqu'à la longueur puis inversez de droite à gauche, deux chapiteau latéral.

Au moment de l'exécution, je suis d'ajouter des images dans la mise en page qui id déclaré en XML à l'intérieur Horizontalview, vérifier cette fonction pour ajouter des images au moment de l'exécution.

public void LLImageView() { 
    imgv = new ImageView[25]; 
    for (int j = 0; j < 25; j++) { 
     imgv[j] = new ImageView(this); 

     img = new ImageView(MainScreenAnim.this); 
     para = new LinearLayout.LayoutParams(imageHeightWidth,imageHeightWidth); 

     para.leftMargin = 10; 
     para.topMargin = 5; 
     imgv[j].setOnClickListener(MainScreenAnim.this); 
     imgv[j].setBackgroundResource(Imgid[j]); 

     layoutHorizontal.addView(imgv[j], para); 

     images.add(Imgid[j].toString()); 

     System.out.println("string [email protected]@@@@@@" + images.get(j)); 
    } 

} 

intérieur OnCreate #

MCObject = new MyCounter(50, 1); 
    MCObject.start(); 

J'ai un nombre fixe d'images à afficher. Pour gérer les événements de clic, regardez sous le code.

public void onClick(View v) { 
    for (int j = 0; j < 25; j++) { 
     if (v == imgv[j]) { 

      //do something 
     } 
    } 

} 
1
  • je TextView avec android: ellipsize = "marquee". son fonctionnement bien, mais l'un des démérite est lorsque la marque continue de fonctionner, puis la taille du tas augmente après 10 à 20 minutes app devenir accident. J'ai donc utilisé l'option RecycleView. Son fonctionnement est très agréable et n'augmente pas la taille du tas.

  • Le texte ainsi que l'image peuvent être définis dans la marque en utilisant recycleView. J'ai perdu deux jours au final j'ai trouvé la bonne solution recycleView. Suivez cette étape.

Etape 1: recycleView Utilisé dans votre mise en page de fragment ou activité

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="10dp" 
    android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/marqueList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white" 
     android:clipToPadding="false" /> 

</LinearLayout> 

Etape 2: Faites une mise en page de l'adaptateur pour recycleView, le nom marque_adapter.xml

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="2dp" 
    android:layout_marginTop="2dp" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="15dp" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/arrow_up"/> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="18sp" 
     android:text="One" 
     /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="15dp" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/arrow_down" 
     /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="18sp" 
     android:text="Two" 
     /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="15dp" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/arrow_up"/> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="18sp" 
     android:text="Three" 
     /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="15dp" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/arrow_down"/> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="18sp" 
     android:text="Four" 
    /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" /> 

</LinearLayout> 

Étape 3: Créer un adaptateur pour recycleVoir son nom MarqueAdapter.java

public class MarqueAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

public class ViewHolderItem extends RecyclerView.ViewHolder { 
    public ViewHolderItem(View view) { 
     super(view); 
} 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    RecyclerView.ViewHolder viewHolder = null; 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.marque_adapter, parent, false); 
    viewHolder = new ViewHolderItem(view); 

    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
} 

@Override 
public int getItemCount() { 
    // You can make count infinite time 
    return 1000; 
}} 

Etape 4: Enfin le faire en vous un fragment ou une activité où recycleView définir

private final Handler mHandler = new Handler(Looper.getMainLooper()); 
    private final Runnable SCROLLING_RUNNABLE = new Runnable() { 
    @Override 
    public void run() { 
     final int duration = 20; 
     final int pixelsToMove = 20; 
     mRecycleMarqueList.smoothScrollBy(pixelsToMove, 0); 
     mHandler.postDelayed(this, duration); 
    } 
}; 

RecyclerView mRecycleMarqueList = (RecyclerView) view.findViewById(marqueList); 
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 
    mRecycleMarqueList.setLayoutManager(layoutManager); 
    MarqueAdapter mMarqueAdapter = new MarqueAdapter(); 
    mRecycleMarqueList.setAdapter(mMarqueAdapter); 
    mHandler.post(SCROLLING_RUNNABLE); 

Enfin son look comme écran ci-dessous court

enter image description here

enter image description here