我想进行可滑动的活动

时间:2019-07-01 11:41:46

标签: java android

我正在开发一个Android项目,我想创建一个具有文本视图的可滑动活动,以便当用户滑动文本视图时值发生变化!

我想使SingleItem类可滑动,以便用户在滑动时可以看到有关该书的更多内容。

这是我的代码!

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

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

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

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

我想使SingleItem类可滑动,以便用户在滑动时可以看到有关该书的更多内容。

2 个答案:

答案 0 :(得分:1)

在活动中使用 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>

然后在您的 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);
        }
    });
}

答案 1 :(得分:0)

也许您想使用SeekBar?必须先刷一下,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>

然后,MainActivity.java:

公共类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) {

        }
    });

    }
}