我有一个RecyclerView
显示两种,View
一种代表用户出版物,另一种代表一种事件出版物.两者都有共同的元素,例如TextView
显示时间戳的元素.所以我创建了一个PublicationViewHolder
将TextView
时间戳记转换为变量并加载它的方法.我的问题是适配器最初加载正确的值,但是当我向下滚动并再次向上滚动时,位置中的值会被其他位置的值更改.这是代码:
public class PublicationViewHolder extends RecyclerView.ViewHolder {
private TextView vTimeStamp;
public PublicationViewHolder(View itemView) {
super(itemView);
this.vTimeStamp = (TextView) itemView.findViewById(R.id.txt_view_publication_timestamp);
}
public void load(Publication publication, int i) {
load(publication);
try {
if (Publication.TYPE_USER_PUBLICATION == publication.getType()) {
load((UserPublication) publication);
} else if (Publication.TYPE_EVENT_PUBLICATION == publication.getType()) {
load((EventPublication) publication);
}
} catch (ClassCastException e) {
throw new RuntimeException("Publication type cast fail. See PublicationViewHolder.");
}
}
public void load(Publication publication) {
vTimeStamp.setText(DateFormatter.getTimeAgo(publication.getTimeStamp()));
}
public …
Run Code Online (Sandbox Code Playgroud) 我开始了解 Jetpack Compose。我将这个应用程序放在一起,探索不同的日常用例,该项目中的每个功能模块都应该解决不同的场景。
\n其中一个功能模块 \xe2\x80\x93 功能chatexample
模块尝试实现一个简单的功能,ViewPager
其中每个页面都是一个Fragment
,第一页“消息”应该显示一个RecyclerView
围绕 . 包裹的分页SwipeRefreshLayout
。现在,我们的目标是使用 Jetpack Compose 来实现这一切。这是我现在遇到的问题:
我用来实现 Pull-To-Refresh 操作的PullRefreshIndicator按预期工作,到目前为止一切看起来都非常简单,但我无法弄清楚为什么 ProgresBar 保持在顶部。
\n到目前为止我已经尝试过了;一直继承Modifier
父母的教诲。Scaffold
确保我明确设置尺寸以适合最大高度和宽度。在whenBox
语句中添加一个空-但到目前为止没有任何效果,我猜我可以删除如果我看到ViewModel不应该刷新,但我不认为那样这是正确的做法。PullRefreshIndicator
为了快速解释我在这里使用的可组合项,我有:
\n<Surface>\n <Scaffold> // Set with a topBar\n <Column>\n <ScrollableTabRow>\n <Tab/> // Set for the first "Messages" tab\n <Tab/> // Set for the second "Dashboard" tab\n </ScrollableTabRow>\n <HorizontalPager>\n // ChatExampleScreen\n <Box> // A Box set …
Run Code Online (Sandbox Code Playgroud) android pager android-scrollable-tabs pull-to-refresh android-jetpack-compose
我的所有单元测试都开始抛出此错误:
No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml
java.lang.NullPointerException
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:375)
at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:117)
at android.content.res.AssetManager.getResourceValue(AssetManager.java)
at android.content.res.Resources.getValue(Resources.java:1347)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:332)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:711)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
at android.support.v7.app.AppCompatDelegateImplBase.__constructor__(AppCompatDelegateImplBase.java:131)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at android.app.Activity.performCreate(Activity.java:6237)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:126)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:340)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:40)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at org.robolectric.util.ActivityController.create(ActivityController.java:133)
at com.si.play.adapter.RosterAdapter2Test.setUp(RosterAdapter2Test.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:176)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at …
Run Code Online (Sandbox Code Playgroud) 所以,我一直在使用JetPack 工具的Android 新分页库部分。
我正在做一个非常基本的演示应用程序,其中显示了我从RandomUser API获得的随机用户配置文件列表。
问题是我已经准备好了一切,它实际上有点工作。
在我的列表片段中,我正在侦听数据库更改:
...
mainViewModel.userProfiles.observe(this, Observer { flowableList ->
usersAdapter.submitList(flowableList) })
...
Run Code Online (Sandbox Code Playgroud)
(我试图与这两个使用Flowables
并Observables
与RxPagedListBuilder
现在,我使用和LiveData
用LivePagedListBuilder
。我想用RxPagedListBuilder
,如果有可能)
在我的 ViewModel 我设置 LivePagedListBuilder
...
private val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(UserProfileDataSource.PAGE_SIZE / 2) //Is very important that the page size is the same everywhere!
.setPageSize(UserProfileDataSource.PAGE_SIZE)
.build()
val userProfiles: LiveData<PagedList<UserProfile>> = LivePagedListBuilder(
UserProfileDataSourceFactory(userProfileDao), config)
.setBoundaryCallback(UserProfileBoundaryCallback(randomUserRepository, userProfileDao))
.build()
...
Run Code Online (Sandbox Code Playgroud)
我已经设置了 DataSource 来从我的数据库中获取数据(如果有的话):
class UserProfileDataSource(val userDao: UserDao): PageKeyedDataSource<Int, UserProfile>() { …
Run Code Online (Sandbox Code Playgroud) android android-room android-livedata android-jetpack android-paging
语境
\n我开始开发一个新项目,并决定从 RxJava 迁移到Kotlin Coroutines。我使用的是 MVVM干净的架构,这意味着我ViewModels
与UseCases
类进行通信,并且这些UseCases
类使用一个或多个Repositories
从网络获取数据。
让我举一个例子。假设我们有一个应该显示用户个人资料信息的屏幕。所以我们有UserProfileViewModel
:
@HiltViewModel\nclass UserProfileViewModel @Inject constructor(\n private val getUserProfileUseCase: GetUserProfileUseCase\n) : ViewModel() {\n sealed class State {\n data SuccessfullyFetchedUser(\n user: ExampleUser\n ) : State()\n }\n // ...\n val state = SingleLiveEvent<UserProfileViewModel.State>()\n // ...\n fun fetchUserProfile() {\n viewModelScope.launch {\n // \xe2\x9a\xa0\xef\xb8\x8f We trigger the use case to fetch the user profile info\n getUserProfileUseCase()\n .collect {\n when (it) {\n is …
Run Code Online (Sandbox Code Playgroud) 我正在尝试以这种方式格式化日期:
Monday 4, November, 2013
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private static String formatDate(Date date) {
Calendar calenDate = Calendar.getInstance();
calenDate.setTime(date);
Calendar today = Calendar.getInstance();
if (calenDate.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)) {
return "Today";
}
today.roll(Calendar.DAY_OF_MONTH, -1);
if (calenDate.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)) {
return "Yesterday";
}
// Guess what buddy
SimpleDateFormat sdf = new SimpleDateFormat("EEEEE d, MMMMM, yyyy");
// This prints "monday 4, november, 2013" ALL in lowercase
return sdf.format(date);
}
Run Code Online (Sandbox Code Playgroud)
但我不想使用某种split
方法或做类似的事情.我是否可以在regexp中包含一些模式,使其在每个单词的开头都是大写的?
更新
我来自一个西班牙裔国家,就像new Locale("es", "ES")
我得到的"martes 7,noviembre,2013",我需要的是"Martes 7,Noviembre,2013".
我们开始在Android应用程序项目中使用Realm.io.但是我们不喜欢Realm.io API的一点是没有任何异步方式来查询数据库.在旧项目中我们使用DbQuery和Sqlite,因此我们习惯在线程或者内部进行数据库查询AsyncTask
.但令人非常不安的是,在所有示例中,查询都是在UiThread
.对于应用程序性能来说这不是一件坏事吗?我们尝试在线程或AsyncTasks中进行查询,但是当我们在UiThread中访问我们的模型对象时,我们得到一个错误,说我们无法在查询它们的线程中访问RealObjects.这是我们的示例代码:
service.getDatabaseHelper()
.with(getActivity()) //Context that I use to instance the Realm instance
.onFinishQuery( new DatabaseHelper.IQueryGetCallback<UserRealm>() {
@Override
public void onGetResult(UserRealm[] results) {
for(UserRealm aUser : results){
//Here is where it crashes
Log.d("Log","Username -> "+aUser.getName());
}
}
})
.getUsersFromDb();
//.......
//In our "DAO" class
public void getUsersFromDb(){
new GetQueryTask() {
@Override
public UserRealm[] onQueryReadyToBeExecuted(Realm realmInstance) {
RealmQuery<UserRealm> query = realmInstance.where(UserRealm.class);
RealmResults<UserRealm> result = query.findAll();
// TODO hacer que devuelva un array
ArrayList<UserRealm> users = new ArrayList<UserRealm>(); …
Run Code Online (Sandbox Code Playgroud) 因此,我正在将项目从使用Android支持库更新为使用AndroidX。我遵循了迁移文档,并更改了每个支持库以供在AndroidX上使用。
一切工作正常,除了一件事,我有一个基本上是AppBarLayout的屏幕,其中包含一个工具栏和一个TabLayout,下面有一个ViewPager,其中包含两个片段,每个片段一个,片段内部仅显示一个列表的项目。这个想法是,如果我向下滚动两个选项卡的任何一个片段,工具栏应该会折叠。在我更新到AndroidX之前,这非常正常。
关于如何解决此问题并继续使用AndroidX库的任何想法?我真的想坚持使用AndroidX库,但是如果它无法正常工作,我将无法使用它。
这是我的gradle / xml文件之前和之后的内容:
在ANDROIDX XML之前:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragmentPetsTabMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/fragmentPetsTabAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/fragmentPetsToolbar"
app:title="@string/menu_transactions"
app:titleTextColor="@color/white"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStartWithNavigation="0dp"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/fragmentPetsTabLayout"
style="@style/CustomTabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="left"
android:background="?attr/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed">
<android.support.design.widget.TabItem
android:id="@+id/fragmentPetsTabOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_transaction_open_tab"/>
<android.support.design.widget.TabItem
android:id="@+id/fragmentPetsTabClosed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_transaction_closed_tab"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/fragmentPetsTabViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
ANDROIDX XML之后:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragmentPetsTabMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/fragmentPetsTabAppBar"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/fragmentPetsToolbar"
app:title="@string/menu_transactions"
app:titleTextColor="@color/white" …
Run Code Online (Sandbox Code Playgroud) android android-viewpager android-coordinatorlayout androidx
语境
所以,我一直在为几个项目使用 MVVM 架构。我仍在努力弄清楚并改进架构的工作方式。我总是使用 MVP 架构,使用常用的工具集,Dagger for DI,通常是多模块项目,Presenter 层被注入了一堆 Interactor/UseCases,每个 Interactor 被注入了不同的 Repositories 来执行后端 API 调用.
现在我已经进入 MVVM,我通过 ViewModel 更改了 Presenter 层,从 ViewModel 到 UI 层的通信是通过 LiveData 完成的,而不是使用 View 回调接口,等等。
看起来像这样:
class ProductDetailViewModel @inject constructor(
private val getProductsUseCase: GetProductsUseCase,
private val getUserInfoUseCase: GetUserInfoUseCase,
) : ViewModel(), GetProductsUseCase.Callback, GetUserInfoUseCase.Callback {
// Sealed class used to represent the state of the ViewModel
sealed class ProductDetailViewState {
data class UserInfoFetched(
val userInfo: UserInfo
) : ProductDetailViewState(),
data class ProductListFetched(
val products: List<Product>
) …
Run Code Online (Sandbox Code Playgroud) 我正在使用SpringMVC,Gson创建一个简单的RESTful服务器并将其部署在GAE中.如果我尝试映射我的请求,一切正常:
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import com.generic.server.model.Widget;
import com.generic.server.services.WidgetService;
import com.google.gson.Gson;
@Component
@Path("/widget")
public class WidgetRestService {
/**
* @return All the widgets info.
* @uri http://localhost:8888/rest/widget/
*/
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)
public @ResponseBody String getAll() {
Gson g = new Gson();
return g.toJson(Arrays.asList(new Widget("BuyerApp", "Buy something now!"),
new Widget("DogSwitcher", "Tired of your dog? Switch it right now!")));
}
}
Run Code Online (Sandbox Code Playgroud)
这打印出所需的结果.但我想摆脱那个恼人的Gson
例子.所以我做了自己的习惯HttpMessageConverter
.
@Component
public …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试禁用ExpandableListView中的突出显示.我尝试将下一个drawable设置为背景,但这不起作用.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/_GREY_Weak"/>
<item android:drawable="@color/_GREY_Weak"
android:state_pressed="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_selected="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_focused="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_checked="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_long_pressable="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_hovered="true" />
<item android:drawable="@color/_GREY_Weak"
android:state_expanded="true" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我尝试将每个状态drawable设置为该颜色,@ null或透明,但仍然相同.以下是我的问题的一些截图.我想摆脱孩子们和父母那样的亮点.
我也triead在ExpandableListView xml中设置属性drawingCacheHint没有运气.
我正在使用Xamarin.Forms开发一个应用程序,我有一个用户可以发布Feed更新的屏幕,此Feed更新包含文本和一些附加文件.我的XAML视图基本上,一个StackLayout
包含ListView
,并Editor
和一对夫妇的Buttons
.问题是我正试图在方法中设置ItemSource
我ListView
的onAppearing
.该方法被称为正常.我正在设置的集合按预期更改.但由于某种原因,ListView
没有更新.ListView
在设置Source
调用Editor
焦点方法后,使工作正常的解决方法.我之所以这样做,是因为我发现当我在设置集合后点击编辑器时,ListView
正确渲染了.但这是一个非常讨厌的解决方法.
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HalliganTL.View.UpdateFeedPage">
<StackLayout Orientation="Vertical" >
<ListView x:Name="AttachedFilesListView" MinimumHeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Button Text="[X]" />
<StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
<Label Text="{Binding Name}" VerticalTextAlignment="Center" FontSize="Medium" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Editor x:Name="UpdateFeedEditor" FontSize="15" Text="{Binding PostText}" VerticalOptions="FillAndExpand" />
<Button Text="Attach file" TextColor="White" BackgroundColor="#77D065" …
Run Code Online (Sandbox Code Playgroud) android ×9
java ×2
listview ×2
mvvm ×2
android-room ×1
androidx ×1
calendar ×1
database ×1
date ×1
formatting ×1
gson ×1
kotlin ×1
pager ×1
realm ×1
rest ×1
robolectric ×1
spring-mvc ×1
unit-testing ×1
xamarin ×1
xml ×1