如何在Android中制作离散的搜索/进度条?

问题描述 投票:0回答:1

如何制作离散进度/搜索栏,例如android中的步进器。就像我们在whatsapp状态故事中看到的一样。

enter image description here

java android progress-bar seekbar stepper
1个回答
0
投票

尽管它由许多库实现,但是我需要在不使用库的情况下实现它。

我使用水平线性布局的视图,并根据需要显示的部分数量或状态故事的数量对其进行动态划分。

activity_main.xml

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

<LinearLayout
    android:id="@+id/layout_views"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:orientation="horizontal"
    android:padding="4dp" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:text="Click" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
Button mButton;
int x=0,height,width,numberOfSections;
LinearLayout layoutViews;
ArrayList<View> viewsList =new ArrayList<>();

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

    //findviews
    layoutViews = findViewById(R.id.layout_views);
    mButton = (Button) findViewById(R.id.button);

    //for getting dimensions of screen
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    height = displayMetrics.heightPixels;
    width = displayMetrics.widthPixels;

    //suppose we have to cut section into 4 parts
    numberOfSections = 10;
    width -= (16 + 16*numberOfSections);    //reducing length of layout by 16dp from left and right and in between 16dp {in between*number of sections)
    width /= numberOfSections;
    for(int i=0;i<numberOfSections;i++){
        View v = new View(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, 10);
        params.setMargins(16,0,0,0);    //giving 16dp internal margin between two views
        v.setLayoutParams(params);
        viewsList.add(v);   //adding views in array list for changing color on click of button

        v.setBackgroundColor(getResources().getColor(colorAccent));
        layoutViews.addView(v);
    }
    //button onclick function
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                buttonClick();
        }
    });
}

public void buttonClick(){
   viewsList.get(x).setBackgroundColor(getResources().getColor(colorPrimaryDark));
   x++;
  }
 }

Voyella!尽管可以在视图中添加过渡动画,但您已经制作了此动态故事进度条。

© www.soinside.com 2019 - 2024. All rights reserved.