【问题标题】:Add views dynamically according to seekbar position根据搜索栏位置动态添加视图
【发布时间】:2018-08-27 13:32:35
【问题描述】:

是否可以在android中使用seek bar的值来为activity添加视图?

例如,如果用户在搜索栏上选择并滚动到值 2,这将在同一个活动中加载两个较小的图像视图

感谢任何帮助。

【问题讨论】:

  • 我认为是。你尝试了什么?
  • 我还没有尝试过任何东西,这只是一个想法。我正在考虑使用以下内容:public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser,特别是可能调用额外图像视图的进度值。我只是不确定如何确保图像视图正确排列并且真正让它工作。
  • 查看this 答案以了解如何做到这一点。我认为您可以使用 GridView 并据此设置其行数或列数。另请查看this
  • @Brummerly 创建带有搜索栏和列表视图的布局,创建带有图像视图的列表视图项目,当用户滚动并选择任何值时,将一些对象插入适配器和宾果游戏,您将获得那么多图像视图.

标签: android seekbar


【解决方案1】:

我一直在研究这个问题,这是我的解决方案。它可能并不完美,如果有需要改进的地方,请告诉我。

This 是结果。

1. 创建一个新项目和download these sample images。将图像文件保存到项目的res/drawable/ 目录中。

2.打开res/layout/activity_main.xml文件并插入以下内容:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:max="10" />
    </LinearLayout>
</ScrollView>

3. 打开MainActivity.java 并为onCreate() 方法插入以下代码:

MainActivity.java

package com.grrigore.dynamicgridview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private int numberOfCurrentImages = 0;
    private int[] images = {
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7,
            R.drawable.sample_0,
            R.drawable.sample_1,
            R.drawable.sample_2
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout = findViewById(R.id.linearLayout);

        SeekBar seekBar = findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                for (int i = numberOfCurrentImages; i < progress; i++) {
                    ImageView imageView = new ImageView(MainActivity.this);
                    imageView.setImageResource(images[numberOfCurrentImages]);
                    linearLayout.addView(imageView);
                    numberOfCurrentImages++;
                }

                if (progress == 0 && linearLayout.getChildCount() > 1) {
                    linearLayout.removeViews(1, numberOfCurrentImages);
                    numberOfCurrentImages = 0;
                }

                if (progress < numberOfCurrentImages) {
                    for (int i = numberOfCurrentImages; i > progress; i--) {
                        linearLayout.removeViewAt(i);
                    }
                    numberOfCurrentImages = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

感谢:

【讨论】:

  • 这可能不是您正在寻找的解决方案,但我认为它提供了一个很好的起点。
猜你喜欢
  • 1970-01-01
  • 2019-07-29
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多