实现此功能的最佳策略是什么:
我有一个水平的RecyclerView卡.每张卡都将实现整个屏幕,但我希望它显示下一张卡的一部分,如果它有多个项目则显示前一张卡.
我知道我可以通过android:layout_width在适配器上设置我的卡来实现这一点,使其具有250dp的特定DP而不是match_parent.但它看起来不是一个合适的解决方案.
这是我的代码:
使用RecyclerView的活动:
class ListPokemon : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val items = createListPokemons()
recyclerView.adapter = PokemonAdapter(items)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.setHasFixedSize(true)
val pagerSnapHelper = PagerSnapHelper()
pagerSnapHelper.attachToRecyclerView(recyclerView)
}
private fun createListPokemons(): List<Pokemon> {
val pokemons = ArrayList<Pokemon>()
pokemons += createPokemon("Pikachu")
pokemons += createPokemon("Bulbasaur")
pokemons += createPokemon("Charmander")
pokemons += createPokemon("Squirtle")
return pokemons
}
private fun createPokemon(name: String) = Pokemon(name = name, height = 1, weight = 69, id = …Run Code Online (Sandbox Code Playgroud) 我有两节课:
public class Questionnaire {
@DatabaseField(generatedId=true, useGetSet=true)
private Long id;
@DatabaseField
private int type;
@DatabaseField
private String title;
@DatabaseField
private String description;
@ForeignCollectionField(eager = true)
private Collection<Question> questions;
// Get and Set omitted
Run Code Online (Sandbox Code Playgroud)
和
public class Question {
@DatabaseField(generatedId=true, useGetSet=true)
private Long id;
@DatabaseField
private int type;
@DatabaseField
private String description;
@DatabaseField(foreign = true, foreignAutoRefresh= true)
private Questionnaire questionario;
//get and set ommited
Run Code Online (Sandbox Code Playgroud)
当我用问题列表保存问卷时.对象是持久的,但我失去了关系.
我以这种方式保存:
ForeignCollection<Question> questions =
getDao(Questionnaire.class).getEmptyForeignCollection("questions");
for(Question question : DataUtil.getAllQuestions()) {
questions.add(question);
}
Questionnaire questionnarie = new …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码创建Heads Up通知.
private static void showNotificationNew(final Context context,final String title,final String message,final Intent intent, final int notificationId, final boolean isHeaderNotification) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
.setSmallIcon(R.drawable.prime_builder_icon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle(title)
.setContentText(message)
.setWhen(0)
.setTicker(context.getString(R.string.app_name));
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.setContentText(message);
if(isHeaderNotification) {
notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, false);
}
notificationBuilder.setContentIntent(fullScreenPendingIntent);
notificationBuilder.setAutoCancel(true);
Notification notification = notificationBuilder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationId, notification);
}
Run Code Online (Sandbox Code Playgroud)
事情是,通知应该出现占据顶部屏幕的一个好部分以引起用户注意,但是几秒钟之后它应该被解雇并且应该出现正常通知.
但是这段代码并没有这样做.通知保持占据所有顶部屏幕,直到用户解雇它.
我正在考虑使用Handler在几秒钟后使用相同的ID创建另一个正常通知,但我想知道是否有更好的方法来执行此操作.
按照WhatsApp的一个例子,模拟我想要的行为.
大家好,我正在创建一个包含行和两个按钮的 Compose 函数。我想以第一个位于屏幕开头、第二个位于边框末尾的方式对齐这些按钮。我怎样才能做到这一点?现在,我有这个:
@Composable
private fun TwoButtons() {
Row(
modifier = Modifier
.fillMaxWidth()
.background(Color.Gray),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
) {
Button(
onClick = { }
) {
Text(text = "One")
}
Button(
onClick = { }
) {
Text(text = "Two")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个结果: