由于两个片段之间的工具栏中的后退按钮的实现困难,我决定开始熟悉Navigation Graph。但我收到一个错误
Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.RelativeLayout{...} is not associated with a Fragment.
这是 .xml 文件,其中包含nav_host_fragment:
<?xml version="1.0" encoding="utf-8"?>
<layout ...
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:clickable="@{viewModel.isSearching ? false : true}"
android:focusable="@{viewModel.isSearching ? false : true}">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recent_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
android:visibility="gone"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="48dp"
android:layout_height="48dp" …Run Code Online (Sandbox Code Playgroud) 当我使用
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很完美。
但如果改为
buildTypes {
release {
minifyEnabled true
useProguard true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Run Code Online (Sandbox Code Playgroud)
我收到解析错误。
我的解析类:
data class SearchItem(
@SerializedName("position") val position: Long,
@SerializedName("user") val user: String?
) {
fun getId(): Long {
return 0L
}
}
data class SearchResponse(
@SerializedName("list") val list: List<SearchItem>,
@SerializedName("has_more") val has_more: Boolean,
@SerializedName("rank_token") val rank_token: String,
@SerializedName("status") val status: String
)
Run Code Online (Sandbox Code Playgroud)
这里我使用gson:
val searchResponse = Gson().fromJson(jsonObject.toString(), SearchResponse::class.java)
Run Code Online (Sandbox Code Playgroud)
在proguard-rules.pro …
我有两个片段,我想使用操作栏中的后退按钮在它们之间创建交互。理想情况下,我希望保存先前片段的状态。我只能找到有关活动的信息。
对于片段我发现了这个
private fun setupBackButton() {
if (activity is AppCompatActivity) {
(activity as AppCompatActivity?)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
}
Run Code Online (Sandbox Code Playgroud)
但它只显示后退按钮,点击没有任何反应。
编辑
在第一个片段中,我将第二个片段称为:
val fragment = UserContentFragment()
fragment.setUser(item.user)
if (fragmentManager != null) {
fragmentManager!!
.beginTransaction()
.replace(R.id.main_layout, fragment)
.addToBackStack(null)
.commit()
}
Run Code Online (Sandbox Code Playgroud)
这是我的UserContentFragment第二个片段:
class UserContentFragment : Fragment() {
private lateinit var user: SearchUser
fun setUser(user: SearchUser) {
this.user = user
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.fragment_user_content, container, false)
val userImage = root.findViewById(R.id.user_img) as ImageView
if …Run Code Online (Sandbox Code Playgroud) android android-fragments kotlin android-actionbar android-navigation
我创建了 stackView 并向其添加了三个标签。我还设置了这些限制
let nameLabel = UILabel()
//set .text, .mode and other for nameLabel
let ellipsisLabel = UILabel()
//set .text, .mode and other for ellipsisLabel
let amountAndMeasureLabel = UILabel()
//set .text, .mode and other for amountAndMeasureLabel
for label in [nameLabel, ellipsisLabel, amountAndMeasureLabel] {
stackView.addArrangedSubview(label)
}
NSLayoutConstraint.activate([
nameLabel.leadingAnchor.constraint(equalTo: stackView.leadingAnchor, constant: 0.0),
nameLabel.trailingAnchor.constraint(equalTo: ellipsisLabel.leadingAnchor, constant: 0.0),
nameLabel.widthAnchor.constraint(equalToConstant: nameLabel.intrinsicContentSize.width),
amountAndMeasureLabel.leadingAnchor.constraint(equalTo: ellipsisLabel.trailingAnchor, constant: 0.0),
amountAndMeasureLabel.trailingAnchor.constraint(equalTo: stackView.trailingAnchor, constant: 0.0),
amountAndMeasureLabel.widthAnchor.constraint(equalToConstant: amountAndMeasureLabel.intrinsicContentSize.width)
])
nameLabel.frame.width // 0
ellipsisLabel.frame.width // 0
amountAndMeasureLabel.frame.width // 0
Run Code Online (Sandbox Code Playgroud)
我将在 .xib 上演示情况以便更好地理解(我不使用这个 .xib) …