我在Activity中带有setOnClickListener的适配器确实可以工作,但是在我的片段中却不能工作。
我有两个带有setOnClickListener的适配器,两者都可以在RecyclerView的“活动”中工作,但是它们都不在我的Fragment的RecyclerView中工作
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myToolbar.title = "My Test"
setSupportActionBar(myToolbar)
var myToggle = object :
ActionBarDrawerToggle(this, myDrawerLayout, myToolbar,
R.string.open, R.string.close) {
override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
invalidateOptionsMenu()
}
override fun onDrawerClosed(drawerView: View) {
super.onDrawerClosed(drawerView)
invalidateOptionsMenu()
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
super.onDrawerSlide(drawerView, slideOffset)
if (slideOffset < 0.6) {
myToolbar.alpha = 1 - slideOffset
}
}
}
myDrawerLayout.addDrawerListener(myToggle)
myDrawerLayout.post {
myToggle.syncState()
}
var myLinearLayoutManager = LinearLayoutManager(this)
myLinearLayoutManager.orientation = LinearLayoutManager.VERTICAL
var myAdapter = MyAdapter(this, MyDataClass.DataProducer.MyList)
myRecyclerView.adapter = myAdapter
myRecyclerView.layoutManager = myLinearLayoutManager
/////////////////////////////////////////////////////////////////////
supportFragmentManager
.beginTransaction()
.replace(R.id.myFrameLayoutMain, MainFragment())
.commit()
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#0068FF"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myToolbar"
android:background="#EF30"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myFrameLayoutMain"
android:background="#61C224"
android:layout_below="@id/myToolbar">
</FrameLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myDrawerLayout"
tools:openDrawer="start"
android:fitsSystemWindows="false"
android:layout_below="@id/myToolbar">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myNavigationView"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FE7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text in NavigationView"
android:textSize="25sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EC67">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myRecyclerView"
android:layout_gravity="center"
android:background="#000000"/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container:
ViewGroup?, savedInstanceState: Bundle?): View? {
var view: View = inflater.inflate(R.layout.fragment_main, container,
false)
var myRe = view.findViewById<RecyclerView>(R.id.myRecyclerViewMain)
var myLinearLayoutManager = LinearLayoutManager(activity)
myLinearLayoutManager.orientation = LinearLayoutManager.VERTICAL
var myAdapter = MyAdapterMain(activity,
MyDataClass.DataProducer.MyList)
myRe.adapter = myAdapter
myRe.layoutManager = myLinearLayoutManager
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myRecyclerViewMain"
android:layout_gravity="center"
android:background="#9C27B0"
android:layout_marginTop="0dp"/>
</FrameLayout>
class MyAdapterMain(var context: Context?, var list: List<MyDataClass>) :
RecyclerView.Adapter<MyAdapterMain.MyViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val view: View =
LayoutInflater.from(context).inflate(R.layout.list_pattern_main, p0, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
p0.myTextHolder.text = MyDataClass.DataProducer.MyList[p1].data
p0.myCircleImgView.setImageResource(R.mipmap.arezo9696)
var font = Typeface.createFromAsset(context?.assets, "BYekan.ttf")
p0.myTextHolder.typeface = font
}
inner class MyViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var myTextHolder = itemView.txtViewPattern!!
var myCircleImgView = itemView.myCircleImageView!!
init {
Log.d("BeForinit MyAdapterMain", "Mahmoud Shokri Test")
myCircleImgView.setOnClickListener {
Toast.makeText(context, "Klicked
Position\n\t\t\t\t$adapterPosition", Toast.LENGTH_SHORT).show()
Log.d("Afterinit MyAdapterMain", "Mahmoud Shokri Test")
}
}
}
}
@Suppress("DEPRECATION")
class MyAdapter(var context: Context?, var list: List<MyDataClass>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val view =
LayoutInflater.from(context).inflate(R.layout.list_pattern_nav, p0, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
p0.myTextHolder.text = MyDataClass.DataProducer.MyList[p1].data
p0.myImgHolder.setImageResource(R.drawable.abc_ic_star_black_36dp)
var font = Typeface.createFromAsset(context?.assets, "BYekan.ttf")
p0.myTextHolder.typeface = font
}
inner class MyViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var myTextHolder = itemView.txtViewPattern!!
var myImgHolder = itemView.imgViewPattern!!
var myRipple = itemView.myRippleView!!
init {
Log.d("BeForinit MyAdapterMain", "Mahmoud Shokri Test")
myRipple.setOnRippleCompleteListener {
Toast.makeText(context, "Klicked
Position\n\t\t\t\t$adapterPosition", Toast.LENGTH_SHORT).show()
Log.d("Afterinit MyAdapterMain", "Mahmoud Shokri Test")
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:layout_marginTop="1dp"
android:gravity="center"
app:cardCornerRadius="10dp"
android:background="#EF7">
<FrameLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/myCircleImageView"
android:src="@mipmap/ic_launcher_round"
android:layout_width="96dp"
android:layout_height="96dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_gravity="end|center_vertical"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtViewPattern"
android:text="@string/my_test_is_this"
android:textSize="30sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="5dp"/>
</FrameLayout>
</android.support.v7.widget.CardView>
对于Fragment,使用setOnClickListener(在AdapterMain中)进行的初始化不起作用。
答案 0 :(得分:-1)
请更改此
var myRe = view.findViewById(R.id.myRecyclerViewMain) as RecyclerView
对此
"starts-with" driver.FindElement(By.XPath("//*[starts-with(@id,'Value')]")).SendKeys("1"); for this XPath `//*[@id="Value_36761_2"]`,