非常容易学的android笔记7

Tue, Dec 20, 2022 2-minute read

Drawables, styles, and themes 功能, OnClick功能

Cards and colors教程

把基本的layout和元素都弄好, 因为是可以滑动的,所以是RecyclerView。

  • activity_main.xml

  • strings.xml

  • Sport.class

    • title
    • info
  • SportsAdapter.class

    • ArrayList mSportData
    • Context mContext
  • MainActivity.class

效果如图

TextView

before style

main_activity.xml

  • RelativeLayout
  • RecyclerView
  • FloatingActionButtun

list_item.xml 加入CardView边框

TextView

CardView

android:adjustViewBounds 这个时为了保持比例

string.xml

这里有播放的新闻标题,内容和图片

TextView

string-array

sport.class修改

加入int imageResource


Glide使用

大量加载图片的时候要用Glide这样的第三方

build.gradle (Module: app)

implementation ‘com.github.bumptech.glide:glide:x.x.x’


SportAdapter.class

  • private ArrayList mSportsData;
  • private Context mContext;
  • private ImageView mSportsImage;

Adapter.getItemCount()得到列表中包含多少个待展示的视图

onCreateViewHolder

创建ViewHolder

return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false));

onBindViewHolder(@NonNull ViewHolder holder, int position)

Adapter会找到目标位置的数据并将其绑定到ViewHolder的视图上。

onCreateViewHolder()方法调用并不频繁。一旦有了够用的ViewHolder,RecyclerView就会停止调用onCreateViewHolder()方法。随后,它会回收利用旧的ViewHolder以节约时间和内存

这里因为是大量图片,所以用了Clide, 加了一个bindTo() 方法,to get the image resource from the Sport object and load it into the ImageView using Glide:

Glide.with(mContext).load(currentSport.getImageResource()).into(mSportsImage);

 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Sport currentSport = mSportsData.get(position);

        holder.bindTo(currentSport);
    }

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

  • private TextView mTitleText;
  • private TextView mInfoText;

public ViewHolder(@NonNull View itemView)

  super(itemView);

  mTitleText = itemView.findViewById(R.id.title);
  mInfoText = itemView.findViewById(R.id.subTitle);
  mSportsImage = itemView.findViewById(R.id.sportsImage);

  itemView.setOnClickListener(this);
}

void bindTo(Sport currentSport) {

    mTitleText.setText(currentSport.getTitle());
    mInfoText .setText(currentSport.getInfo());
    Glide.with(mContext).load(currentSport.getImageResource()).into(mSportsImage);
}

void onClick(View v)

  Sport currentSport = mSportsData.get(getAdapterPosition());

  Intent detailIntent = new Intent(mContext, DetailActivity.class);
  detailIntent.putExtra("title", currentSport.getTitle());
  detailIntent.putExtra("image_resource", currentSport.getImageResource());

  mContext.startActivity(detailIntent);
        
TextView

SportAdapter

TextView

SportAdapter


onClick() 方法

过时的layout中设置的android:onClick

用这个会化线,应该被启用了,方法就是在activity中写这个方法

Button和ImageView中控件结合的

activity中申明变量后,onCreate方法中根据id, 设置

setOnClickListener(new View.OnClickListener() {)

也可以用lambda

myButton.setOnClickListener(v -> Log.d(“Click”, “Button clicked”); );

TextView中的onClick()

在ViewHolder中使用

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

实现onClick(View v)方法


滑动图片

TextView

swipe

TextView

swipe

这里划掉的才是这个版本的。所以gridColumnCount也是为了后面的内容

MainActivity

  • private RecyclerView mRecyclerView;
  • private ArrayList mSportsData;
  • private SportAdapter mAdapter;

用ItemTouchHelper

ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper
                .SimpleCallback(0, ItemTouchHelper.LEFT |
                ItemTouchHelper.RIGHT) {

0 for the supported move directions and ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT for the supported swipe directions

Change the first argument of the SimpleCallback from 0 to include every direction, since we want to be able to drag and drop anywhere:

ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper
.SimpleCallback(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | 
ItemTouchHelper.DOWN | ItemTouchHelper.UP, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

initializeDate()

这里注意下要回收 sportsImageResources.recycle();