- 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/
这里有这样的问题,但我尝试了解决方案,但在我的情况下这不起作用。我有一个通过单击打开的表单,然后必须通过单击此表单内的 x 将其关闭。 This variant不适合,因为 cookie 在关闭表单后
我有以下代码: function sdefaults() { alert("test"); } var btnpos, sbtn; btnpos = document.getElementsBy
我正在学习 javascript 和 php。我创建了一个联系表单,我希望按下提交按钮时可以完成两件事: 将数据提交给我(这部分正在运行) 阅读我的 onclick 函数(这部分不起作用) 我正在
我的页面有多个div元素,有显示切换功能。 onclick = "document.getElementById('light').style.display='block';document.ge
为什么不同? document.getElementById('click').onclick = a.replaceChild(c,b) --- 在我不点击按钮的情况下替换元素 但 onclick=
我以为在javascript中绑定(bind)点击事件是通过node.onclick来完成的,Chrome/Firefox似乎也同意我的看法,但我看到它写成.onClick here 3个人4次,所以
基本上,我想创建一个侧边栏,其中会有很多联系人。每个联系人都会出现在一个列表中。列表中有两个 onclick 函数。列表中两个 onclick 函数是:第一次点击-点击列表后它应该打开另一个侧边栏(我
我是 Javascript 的新手,尤其是在 JSON 和 Jquery 上。我已经成功实现了 FullCalendar 的月 View ,但是无论如何要根据用户单击月 View 的日期来显示或弹出日
给定一些简单的 HTML,例如,其中一个元素具有 onclick 函数,并且它的子元素也具有 onclick 函数: 正确的方法是什么,以便当用户单击子元素时,仅执行子元素的 oncli
我有一个功能,当您单击一个按钮时,TinyMCE 框内的选定文本被包装在 span 中标签。 这是这样做的: var apolo = '' + sel + ''; tinyMCE.activeEdit
我尝试使用 JavaScript 将 onclick 事件替换为其他 onclick 事件: 旧 onClick 事件: $('#myButton').click(function(){ a
我正在尝试删除一个函数中的事件监听器,该函数与 onclick 调用的函数相同。 下面的例子说明了我的问题。我尝试使用这种构造的原因是将函数传递给 View 对象,以便它可以将其作为 onclick
这是一个相当简单的可访问性问题。 假设我们有一个切换链接/按钮,单击该链接/按钮即可切换移动菜单。对于这样的按钮,哪种格式更易于访问? 我们总共有 4 个选项,用 分隔 button标签与 a标签,
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make onclick automatically through onload function
我需要能够改变一个 id 的 onclick 事件,这样一旦它被点击,一旦它执行一个改变 onclick 事件的函数 这是我的代码:Javascript: function showSearchBar
有谁知道如何将 onClick 分配给执行一个操作的对象,然后当用户点击同一对象时,该操作被反转? $('#image').click(function() { $('#foo').css({
我重写了 onClick(View) 方法,以便在单击时 View 大小会发生变化。View 是一个显示浏览器网页的 webView。我需要点击会改变大小,而且浏览器会响应(转到被点击的链接)。在目前
我有这个 HTML 代码: APPLY JS: $('#changeType').click(function(){ $('#discountCode').attr('type','pass
我想用 onclick 创建一个函数,如果我按下 div 就会显示内容。如果我点击该内容,它会给我“开始”表单。 让我向您展示代码: HTML: click Javascript: var div =
我编写了一个函数,用于使用超链接的占位符打开/关闭 div。 Open 然后 onclick 事件打开 div。 我有这个onclick来关闭它: Close 我想要做的是为在两个 onclick 事
我是一名优秀的程序员,十分优秀!