3

我需要有文本视图和图像视图的选框,如图所示。textview 和 imageview 将在哪里移动 在 textview 或 Imageview 的 onclick 处,它应该打开一个活动。有人可以提出一种方法吗?

ImageView 应该是可点击的。

4

3 回答 3

3

我在 Horizo​​ntalView 中的图像视图上实现了选取框,类似于您在问题中描述的,使用CountDownTimer

对于您的问题,您必须在 Horizo​​ntalView 内的一个布局内texviewImagview在 Horizo​​ntalView 上应用选取框,

查看代码#

    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) {

    }

}

它会从左到右给出选框直到长度,然后从右到左反转,两侧选框。

在运行时,我在 Horizo​​ntalview 的 xml 中声明的布局中添加图像,检查此函数以在运行时添加图像。

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 arraylist@@@@@@@" + images.get(j));
    }

}

内部 OnCreate#

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

我有固定数量的图像要显示。要处理点击事件,请查看下面的代码。

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

            //do something
        }
    }

}
于 2012-10-09T11:39:27.863 回答
1

创建自定义视图

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

并且您可以在此布局中拥有任何类型的视图,您可以在 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>
于 2012-11-16T06:03:57.183 回答
1
  • 我将 TextView 与 android:ellipsize="marquee" 一起使用。它运行良好,但缺点之一是当品牌继续运行时,堆大小在 10 到 20 分钟后增加,应用程序崩溃。所以我使用了 RecycleView 选项。它的工作非常好并且不会增加堆大小。

  • 可以使用recycleView 在marque 中设置文本和图像。我浪费了两天终于找到了正确的解决方案recycleView。按照这个步骤。

第 1 步:在您的片段或活动布局中使用 recycleView

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

Step2:为recycleView做一个适配器布局,命名为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>

Step3:为recycleView制作Adapter,命名为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;
}}

第4步:最后在recycleView定义的片段或活动中执行此操作

  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);

最后它看起来像下面的屏幕短

在此处输入图像描述

在此处输入图像描述

于 2016-10-27T04:57:21.557 回答