- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从 recyclerView 中的另一个 fragment 打开一个 fragment ,但每次我选择一个 recyclerViewItem 时,我总是得到该 fragment 的“找不到 id 的 View ”。我尝试了很多解决方案,但到目前为止我的努力都失败了。
主要 Activity
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
FragmentManager fragmentManager;
android.support.v4.app.FragmentTransaction fragmentTransaction;
ImageCache imageCache;
List<Product> product = new Select().from(Product.class).queryList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FlowManager.init(getApplicationContext());
final MyApp app = new MyApp(this);
imageCache = new ImageCache();
/**
*Setup the DrawerLayout and NavigationView
*/
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationView = (NavigationView) findViewById(R.id.navigationView);
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_home) {
android.support.v4.app.FragmentTransaction hfragmentTransaction = fragmentManager.beginTransaction();
hfragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_products) {
android.support.v4.app.FragmentTransaction pfragmentTransaction = fragmentManager.beginTransaction();
pfragmentTransaction.replace(R.id.containerView, new ProductsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_news) {
android.support.v4.app.FragmentTransaction nfragmentTransaction = fragmentManager.beginTransaction();
nfragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_companies) {
android.support.v4.app.FragmentTransaction cfragmentTransaction = fragmentManager.beginTransaction();
cfragmentTransaction.replace(R.id.containerView, new CompaniesFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_save_db) {
app.createDatabase();
app.setLastUpdatedDate();
imageCache.imageDownloader(getApplicationContext());
}
if (menuItem.getItemId() == R.id.nav_item_update_db) {
app.updateOutdatedDatabase(app.getLastUpdatedDate());
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name,
R.string.app_name);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
for (int i = 0; i < product.size(); i++) {
//Log.i("Name", product.get(i).getProductName());
Log.i("dasd", product.get(i).getProductImage());
}
//app.createDatabase();
//ImageView imageView = (ImageView) findViewById(R.id.image);
/*Picasso.with(this)
.load(new File(product.get(2).getProductImage()))
.networkPolicy(NetworkPolicy.OFFLINE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(imageView);
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_refresh) {
imageCache.imageDownloader(this);
}
return super.onOptionsItemSelected(item);
}}
MyRecyclerViewAdapter
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
private ArrayList<HomeObject> mDataset;
private Context context;
private static MyClickListener myClickListener;
private String[] postTypeRef = new String[] {"Νέα","Κίνδυνοι","Προειδοποιήσεις"};
public MyRecyclerViewAdapter(Context context, MyClickListener myClickListener) {
this.context = context;
this.myClickListener = myClickListener;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView label;
TextView company;
TextView description;
ImageView imageDrawable;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
company = (TextView) itemView.findViewById(R.id.textView2);
description = (TextView) itemView.findViewById(R.id.textView3);
imageDrawable = (ImageView) itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getLayoutPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<HomeObject> myDataset) {
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_home_item, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.label.setText(mDataset.get(position).getLabel());
holder.company.setText(mDataset.get(position).getCompany());
holder.description.setText(mDataset.get(position).getTextDescr());
if (Arrays.asList(postTypeRef).contains(mDataset.get(position).getLabel())){
Picasso.with(MyApp.applicationContext).load(mDataset.get(position)
.imageIDFetch()).into(holder.imageDrawable);
}
else{
Picasso.with(MyApp.applicationContext).load(new File(mDataset.get(position)
.getImageCached())).into(holder.imageDrawable);
}
}
public void addItem(HomeObject dataObj, int index) {
mDataset.add(dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}}
最后一个 HomeFragment
public class HomeFragment extends Fragment {
private int productsListCounter = 0;
private int postsListCounter = 0;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static String LOG_TAG = "RecyclerViewActivity";
private Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_layout, null);
context = container.getContext();
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.activity_main_swipe_refresh_layout);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.addItemDecoration(
new HorizontalDividerItemDecoration.Builder(getActivity())
.color(Color.rgb(11, 122, 11))
.margin(15, 15)
.build());
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet());
PreCachingLayoutManager layoutManager = new PreCachingLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setExtraLayoutSpace(DeviceUtils.getScreenHeight(getActivity()));
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mAdapter = new MyRecyclerViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
mSwipeRefreshLayout.setRefreshing(false);
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Log.i(LOG_TAG, " Clicked on Item " + position);
Fragment fragment = new ProductFragment();
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.main_activity, fragment);
transaction.addToBackStack(null);
transaction.commit();
mAdapter = new MyRecyclerViewAdapter(context,this);
}
});
}
private ArrayList<HomeObject> getDataSet() {
ArrayList results = new ArrayList<HomeObject>();
List<Product> products = new Select().from(Product.class).queryList();
List<Post> posts = new Select().from(Post.class).queryList();
for (productsListCounter = 0; productsListCounter<products.size(); productsListCounter++){
Company companyIDQuery = new Select().from(Company.class)
.where(com.raizlabs.android.dbflow.sql.builder.Condition.column(Post$Table.COMPANYID)
.eq(posts.get(productsListCounter).getCompanyID())).querySingle();
HomeObject homeContainer = new HomeObject(
"Προίον: " + products.get(productsListCounter).getProductName(),
"Εταιρεία : " + companyIDQuery.getCompanyName(),
products.get(productsListCounter).getProductDescr(),
products.get(productsListCounter).getProductImage(),
products.get(productsListCounter).getUpdatedAT());
results.add(productsListCounter, homeContainer);
}
for (postsListCounter = 0; postsListCounter<posts.size(); postsListCounter++){
PostType postTypeIDQuery = new Select().from(PostType.class)
.where(com.raizlabs.android.dbflow.sql.builder.Condition.column(Post$Table.POSTTYPEID)
.eq(posts.get(postsListCounter).getPostTypeID())).querySingle();
Company companyIDQuery = new Select().from(Company.class)
.where(com.raizlabs.android.dbflow.sql.builder.Condition.column(Post$Table.COMPANYID)
.eq(posts.get(postsListCounter).getCompanyID())).querySingle();
HomeObject homeContainer = new HomeObject(
postTypeIDQuery.getPostTypeDescr(),
"Εταιρεία : " + companyIDQuery.getCompanyName(),
posts.get(postsListCounter).getPostText(),
"",
posts.get(postsListCounter).getUpdatedAT());
results.add(productsListCounter + postsListCounter, homeContainer);
}
Collections.sort(results, Collections.reverseOrder());
return results;
}}
因此,当我单击 HomeFragment recyclerViewItem 时,我想打开另一个包含数据和位置的 fragment 。此外,所有 fragment 都属于 MainActivity 布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:id="@+id/main_activity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Drawer With Swipe Tabs" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/navigationView"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
提前致谢!
最佳答案
你在 HomeFragment
行下面添加了错误的语句
transaction.add(R.id.main_activity, fragment);
例子如下而不是 R.id.main_activity
添加 R.id.containerView
它将解决您的问题
Bundle bundle = new Bundle();
bundle.putString("city", "city");
bundle.putBoolean("demo_access", true);
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
PlantFragment plantFragment = new PlantFragment();
plantFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.containerView, plantFragment, "temp");
fragmentTransaction.addToBackStack("pref");
fragmentTransaction.commit();
关于android - RecyclerView onClick 总是返回 "No view found for id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269384/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!