ListView 控件
- 自定义里面的内容
1.创建一个类继承ArrayAdapter,然后重写super(context,super.ListView,objects)
public class CatPicture extends ArrayAdapter {
private int resourceId;
public CatPicture(@NonNull Context context, int resourceId, List<Cat> catList) {
super(context, resourceId, catList);
// the target is that the layout is filled into current environment
this.resourceId = resourceId;
}
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// getItem();在幕后被添置objects;所以getItem(position)可以得到一个实例对象
Cat cat = (Cat) getItem(position); //get current instance of CarPicture
//获取ListView的元素布局对象
/*View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); 未优化*/
//优化
View view;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
}else{
view = convertView;
}
ImageView catPhoto = view.findViewById(R.id.item);
TextView catName = view.findViewById(R.id.text);
catPhoto.setImageResource(cat.getPhotoId());
catName.setText(cat.getName());
return view;
}
}
class Cat {
private String name;
private int photoId;
public Cat(String name, int photoId) {
this.name = name;
this.photoId = photoId;
}
public String getName() {
return name;
}
public int getPhotoId() {
return photoId;
}
}
- 在主类之中给ListView添加adapter
CatPicture picture = new CatPicture(MainActivity.this,R.layout.sample_list_1,list);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(picture);
- 为了加快加载依赖包速度,先配置一波ali镜像
在app外面的build.gradle文件中将google() jcenter()换成
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
RecyclerView的基本用法
…空着候补
Fragment布局
创建静态的fragment.xml文件和对应的**.java文件
简单配置如下:
.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="23dp"
/>
</LinearLayout>
.java文件
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//inflater.inflater 将left_fragment.xml布局加载到容器
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
right_fragment同理....
main.xml配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:layout_width="0dp"
android:id="@+id/left_fragment"
android:name="com.example.fragmenttwo.LeftFragment"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment android:id="@+id/right_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- Fragment也可以和activity 进行通信,Fragment提供一个类似方法,findFragmentById();得到一个碎片里的方法。
RightFragment rightFragment = (RightFragment)findFragmentById(R.id.right_fragment);
调用FragmentMananger的方法可以得到一个Fragment的一个实例对象,然后就可以调用fragment的方法了。 - 同样在fragment里面也可以调用一个getActivity()
MainActivity main = getActivity();
有了activity的实例之后,在碎片调用活动的方法就轻而易举了。另外当碎片需要使用Context对象时,也可以调用getActivity()方法,本身就是一个Activity对象。 - fragment和fragment的通信。
两个fragment的和活动关联,以活动为介质,进行通信。