Fragment.java
Adapter.java
CustomView.java
fragment_main.xml
item_fragment.xml
Fragment.java
Fragment.java
/**
*Contrôle de la température Classe d'écran de fragment
*/
class AdjustTempFragment : Fragment() {
/**
*Vue en grille
*/
private lateinit var gridView: GridView
/**
*Une sorte de tableau de rétention d'informations
*/
private lateinit var values: Array<HogeEnum>
/**
*Adaptateur de réseau
*/
private lateinit var gridAdapter: GridAdapter
/**
*Objet compagnon
*/
companion object {
/**
*le contexte
*/
lateinit var context: Context
/**
*Méthode de génération d'instance
*/
fun createInstance(context: Context): Fragment {
val fragment = Fragment()
this.context = context
return fragment
}
}
/**
*Méthode d'initialisation
*/
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_main, container, false)
}
/**
*Méthode principale
*-Voir le processus de dessin
*・ Réglage du traitement de transition d'écran
*/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Dessiner une vue personnalisée
this.gridView = view.findViewById(R.id.grid)
this.values = HogeEnum.values()
this.adapter = Adapter(this.context, this.values)
this.gridView.adapter = this.adapter
//Transition vers l'écran de réglage
val image: ImageView = view.findViewById(R.id.setting_button)
image.setOnClickListener {
val intent = Intent(activity, SettingActivity::class.java)
startActivity(intent)
}
}
}
Adapter.java
class Adapter : BaseAdapter {
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
/**
*le contexte
*/
private lateinit var context: Context
/**
*Une sorte de tableau de rétention d'informations
*/
private var values: Array<HogeEnum>
/**
*constructeur
*/
constructor(con: Context?) {
if (con != null) {
this.context = con
}
}
/**
*Nombre d'éléments dans le tableau
*/
override fun getCount(): Int {
return this.values.size
}
/**
*Éléments du tableau
*/
override fun getItem(p0: Int): Any {
return this.values[p0]
}
/**
*Voir la méthode de dessin
*/
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val itemLayoutView = p1 ?: View.inflate(this.context, R.layout.item_fragment, null)
var viewHolder = itemLayoutView.tag
if (viewHolder == null) {
viewHolder = ViewHolder()
viewHolder.customView = itemLayoutView.custom_view as CustomView
(itemLayoutView.custom_view as CustomView).initView(
this.values[p0].index()
)
itemLayoutView.tag = viewHolder
}
return itemLayoutView
}
}
/**
*Classe de spectateur
*/
class ViewHolder {
lateinit var customView: CustomView
}
CustomView.java
class CustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
/**
*Méthode d'initialisation
*/
@SuppressLint("ResourceAsColor")
fun initView() {
var lp = LayoutParams(200, 200)
var imageView = ImageView(context)
imageView.layoutParams = lp
this.addView(imageView)
}
}
fragment_main.xml
<?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">
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3">
</GridView>
</LinearLayout>
item_fragment.xml
<?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">
<jp.co.hoge.project.views.CustomView
android:id="@+id/custom_view"
android:layout_width="200dp"
android:layout_height="200dp">
</jp.co.hoge.project.views.CustomView>
</LinearLayout>
Recommended Posts