简体   繁体   English

我想进行可滑动的活动

[英]I want to make a swipeable activity

I'm working on an Android project and I want to make a swipeable activity that has a textview in it so When the user swipes the textview value changes! 我正在开发一个Android项目,我想创建一个可滑动活动,其中包含textview,因此当用户滑动textview值时,它会发生变化!

I want to make the SingleItem class swipeable so that the user can see more content about the book when they swipe. 我想使SingleItem类可滑动,以便用户在滑动时可以看到有关该书的更多内容。

Here is my code!!! 这是我的代码!!!

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
List<Book> books;

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

    books = new ArrayList<>();
    books.add(new Book("Rich Dad Poor Dad", "Rich Dad Poor Dad is a 1997 book written by Robert Kiyosaki and Sharon Lechter.", "Robert Kiyosaki"));
    books.add(new Book("Awaken The Giant Within", "Wake up and take control of your life! From the bestselling author of Inner Strength, Unlimited Power, and MONEY Master the Game", "Anthony Robbins"));

    mRecyclerView = findViewById(R.id.my_recyclerview);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new RecyclerViewAdapter(books, this);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
}
}

RecyclerViewAdapter.java RecyclerViewAdapter.java

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

private List<Book> bookList;
private Context mContext;

public RecyclerViewAdapter(List<Book> bookList, Context mContext) {
    this.bookList = bookList;
    this.mContext = mContext;
}

@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view ;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.item_list,viewGroup,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder viewHolder, final int i) {
    viewHolder.bookTitle.setText(bookList.get(i).getmTitle());
    viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, SingleItem.class);
            intent.putExtra("Description", bookList.get(i).getmDescription());
            intent.putExtra("Author", bookList.get(i).getmAuthor());
            mContext.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return bookList.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public View view;
    public TextView bookTitle;
    public CardView cardView;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        bookTitle = itemView.findViewById(R.id.book_title);
        cardView = itemView.findViewById(R.id.my_card_view);
        view = itemView;
    }
}

SingleItem.java SingleItem.java

public class SingleItem extends AppCompatActivity {

private TextView bookDescription, bookAuthor;

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

    bookDescription = findViewById(R.id.book_description);
    bookAuthor = findViewById(R.id.book_author);

    // Recieve data
    Intent intent = getIntent();
    String Desc = intent.getExtras().getString("Description");
    String Author = intent.getExtras().getString("Author");

    bookDescription.setText(Desc);
    bookAuthor.setText(Author);
}

Book.java Book.java

public class Book {

private String mTitle;
private String mDescription;
private String mAuthor;

public Book(){
}

public Book(String mTitle, String mDescription, String mAuthor) {
    this.mTitle = mTitle;
    this.mDescription = mDescription;
    this.mAuthor = mAuthor;
}

public String getmTitle() {
    return mTitle;
}

public void setmTitle(String mTitle) {
    this.mTitle = mTitle;
}

public String getmDescription() {
    return mDescription;
}

public void setmDescription(String mDescription) {
    this.mDescription = mDescription;
}

public String getmAuthor() {
    return mAuthor;
}

public void setmAuthor(String mAuthor) {
    this.mAuthor = mAuthor;
}
}

I want to make the SingleItem class swipeable so that the user can see more content about the book when they swipe. 我想使SingleItem类可滑动,以便用户在滑动时可以看到有关该书的更多内容。

Use SwipeRefreshLayout in your activity to Swipe down and update textview 在活动中使用SwipeRefreshLayout向下滑动并更新textview

    <android.support.v7.widget.SwipeRefreshLayout
    android:id="@+id/pullToRefresh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.SwipeRefreshLayout>

Then in your Activity.java 然后在您的Activity.java中

TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
mTextView=findViewById(R.id.textView);
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mTextView.setText("My new text") // update here
            pullToRefresh.setRefreshing(false);
        }
    });
}

Maybe you'd like to use an SeekBar? 也许您想使用SeekBar? It has to be swiped and TextView has to be showing text, not vice versa. 必须先刷一下,TextView必须显示文本,反之亦然。 Set your SeekBar and TextView: 设置您的SeekBar和TextView:

<?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="match_parent"
              android:orientation="vertical"
              android:padding="@dimen/small_padding">

    <TextView
            android:id="@+id/text_progress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"/>

</LinearLayout>

Then, MainActivity.java: 然后,MainActivity.java:

public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展了AppCompatActivity {

TextView textView;
SeekBar seekBar;

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

    textView = findViewById(R.id.text_progress);
    seekBar = findViewById(R.id.seek_bar);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textView.setText("Your progress is " + progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使SwipeRefreshLayout不可滑动? - How to make a SwipeRefreshLayout not swipeable? 如何使片段动画可滑动 - How to make fragment animation swipeable 在活动中创建一个名称为Category的类,我想在Android的主活动中调用该类的对象 - Make a class with name Category in a activity and I want to call the object of this class in the main activity on Android 将数据从活动传递到可滑动查看的片段 - Passing data from activity to swipeable view fragment 我正在尝试在android中制作可滑动的选项卡布局,但在操作栏中获取空值 - I am trying to make the swipeable tab layout in android but getting null value in action bar 我想在Android活动中制作一个带有图像的可滚动文本列表。 我应该使用什么? - I want to make a scrollable list of text with images in an Android Activity. What should I use? 在Android中制作可滑动标签页布局的最简单方法 - Simplest way to make a swipeable tab layout in Android 我想制作一个 recyclerview 来显示来自 SQLITE 数据库的新活动的数据 - I want to make a recyclerview that shows data to a new Activity from SQLITE database 从可滑动选项卡视图中的活动中调用片段方法 - Calling Fragment Method from Activity in Swipeable Tab Views 我想在同一个活动中使用广播和服务 - I want to use broadcast and services on the same activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM