我正在开发一个 Android 项目,在该项目中,我使用“SuperSlim”框架来创建 Notes(自定义类)的 GridView 以及要显示的数据。数据库中的注释与部分(自定义类)具有多对一的关系。而 Sections 又与 Canvas 具有多对一的关系。
部分的所有信息,注释都是作为列表从服务器动态检索的。
现在我可以显示部分的 GridView ,并将文本信息(如部分名称等)放入网格中。 出于测试目的,我还插入了从笔记中静态检索的文本。 我是 Android 编程的新手,所以如果代码看起来乱七八糟,请不要介意。
现在这些是我面临的问题:
1) 如何显示部分网格,在显示的每个部分中,我想显示一个注释网格。由于存在一对多关系,因此每个部分可以有多个注释。这是我的主要问题。
2) 通过显示上面的内容,我想保持 SectionName 字段可编辑,并且我有一个 REST 方法可以用来编辑 Section-name,但我也需要 section-id。单击即可使用,我想维护它。
3) Section 中显示的笔记网格应该是可点击的,这样我以后可以打开类似于 Modal 的东西,这样用户就可以阅读整个笔记并进行编辑。
下面的截图显示了我目前的情况:
左侧移动是网格部分列表的原始外观。我对其进行了修改,以显示更多信息,仅用于测试,并使用 SuperSlim 更正确地添加信息。
请注意,目前在代码中,我正在为硬编码部分静态调用 NotesList 方法。这是不希望的。最后代码:
组节 Activity :
public class GroupSectionActivity extends ActionBarActivity {
private SectionServiceImpl sectionService = new SectionServiceImpl();
private static volatile List<RestSection> restSectionList = new ArrayList<>();
private static volatile Long groupAccountId;
private static volatile Integer canvasid;
static final String msectionname = "msectionname";
static final String msectionid = "msectionid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_section_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
groupAccountId = extras.getLong("groupid");
canvasid = extras.getInt("canvasid");
}
if(savedInstanceState == null){
getFragmentManager().beginTransaction().add(R.id.container, new NoteFragments(), "msectionname").commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null){
setSupportActionBar(toolbar);
}
restSectionList = this.sectionService.getSectionByCanvas(canvasid);
ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<HashMap<String, String>>();
for (RestSection restSection : restSectionList) {
HashMap<String, String> sectionDisplay = new HashMap<>();
sectionDisplay.put("msectionid", String.valueOf(restSection.getMsectionid()));
sectionDisplay.put("msectionname", restSection.getMsectionname());
restSectionArrayList.add(sectionDisplay);
}
/* listView = (ListView) findViewById(R.id.seclist);
sectionLazyAdapter = new SectionLazyAdapter(this, restSectionArrayList);
listView.setAdapter(sectionLazyAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int sectionId = restSectionList.get(position).getMsectionid();
Log.d("Sectionid is ", String.valueOf(sectionId));
*//*Intent intent = new Intent(GroupSectionActivity.this, GroupSectionActivity.class);
intent.putExtra("groupid", groupAccountId);
intent.putExtra("sectionid", sectionId);
startActivity(intent);
finish();*//*
}
});
addSectionButton = (Button) findViewById(R.id.sectionAddButton);
addSectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int val = addGroupSection();
if (val == 0) {
sectionName.setError("Section Name cannot be null");
} else {
sectionName.clearComposingText();
sectionName.clearAnimation();
sectionName.setText("");
Toast.makeText(getApplicationContext(), "Section added", Toast.LENGTH_LONG).show();
}
}
});*/
}
public Integer addGroupSection(){
/* sectionName = (EditText) findViewById(R.id.sectionNameTextField);
if (!(sectionName.getText().toString().isEmpty())) {
RestSection restSection = new RestSection();
restSection.setMsectionname(sectionName.getText().toString());
return this.sectionService.addGroupSection(restSection,canvasid);
}
*/
return 0;
}
@Override
public void onBackPressed() {
Intent intent = new Intent(GroupSectionActivity.this, GroupCanvasActivity.class);
intent.putExtra("groupid", groupAccountId);
startActivity(intent);
finish();
}
private NoteFragments getSectionsFragment() {
return (NoteFragments) getFragmentManager().findFragmentByTag(msectionname);
}
}
SectionLazyAdapter :
public class SectionLazyAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public SectionLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_group_section, null);
TextView sectionName = (TextView)vi.findViewById(R.id.sectionname); // title
HashMap<String, String> sectionList = new HashMap<String, String>();
sectionList = data.get(position);
sectionName.setText(sectionList.get(GroupSectionActivity.msectionname));
return vi;
}
}
注意适配器:
public class NoteAdapters extends RecyclerView.Adapter<NoteViewHolder> {
private NoteServiceImpl noteService = new NoteServiceImpl();
private static final int LINEAR = 0;
private final Context mContext;
private SectionServiceImpl sectionService = new SectionServiceImpl();
List<RestSection> restSectionList = new ArrayList<>();
private final ArrayList<LineItem> mItems;
public NoteAdapters(Context context, int headermode) {
mContext = context;
int sectionManager = -1;
int sectionFirstPosition = 0;
mItems = new ArrayList<>();
restSectionList = this.sectionService.getSectionByCanvas(2500);
for (int i = 0; i < restSectionList.size(); i++) {
String header = restSectionList.get(i).getMsectionname();
RestNote restNote = this.noteService.getFirstNoteForSection(restSectionList.get(i).getMsectionid());
mItems.add(new LineItem(header, true, sectionManager, sectionFirstPosition, restNote.getMnotetext()));
}
}
@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_group_section, parent, false);
return new NoteViewHolder(view);
}
@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
final LineItem item = mItems.get(position);
final View itemView = holder.itemView;
holder.bindText(item.text);
holder.bindNoteData(item.otherText);
final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams());
lp.setSlm(item.sectionManager == LINEAR ? LinearSLM.ID : GridSLM.ID);
lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width));
lp.setFirstPosition(item.sectionFirstPosition);
itemView.setLayoutParams(lp);
}
@Override
public int getItemCount() {
return mItems.size();
}
/* @Override
public void onClick(View v) {
if(v instanceof ImageView){
Log.d("Image","Clicked");
} else {
Log.d("Text","Clicked");
}
}*/
private static class LineItem {
public int sectionManager;
public int sectionFirstPosition;
public boolean isHeader;
public String text;
public String otherText;
public LineItem(String text, boolean isHeader, int sectionManager,
int sectionFirstPosition, String otherText) {
this.isHeader = isHeader;
this.text = text;
this.sectionManager = sectionManager;
this.sectionFirstPosition = sectionFirstPosition;
this.otherText = otherText;
}
}
}
笔记 fragment :
public class NoteFragments extends Fragment {
private ViewHolder mViews;
private NoteAdapters noteAdapters;
private int mHeaderDisplay;
private boolean mAreMarginsFixed;
private Random mRng = new Random();
private Toast mToast = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.section_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViews = new ViewHolder(view);
mViews.initViews(new LayoutManager(getActivity()));
noteAdapters = new NoteAdapters(getActivity(), mHeaderDisplay);
mViews.setAdapter(noteAdapters);
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
private static class ViewHolder {
private final RecyclerView mRecyclerView;
public ViewHolder(View view) {
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
}
public void initViews(LayoutManager lm) {
mRecyclerView.setLayoutManager(lm);
}
public void scrollToPosition(int position) {
mRecyclerView.scrollToPosition(position);
}
public void setAdapter(RecyclerView.Adapter<?> adapter) {
mRecyclerView.setAdapter(adapter);
}
public void smoothScrollToPosition(int position) {
mRecyclerView.smoothScrollToPosition(position);
}
}
}
笔记 View 持有者:
public class NoteViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private TextView noteData;
private ImageView imageView;
public NoteViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.sectionname);
imageView = (ImageView) itemView.findViewById(R.id.sectionimage);
noteData = (TextView) itemView.findViewById(R.id.noteText);
}
public void bindText(String text){
textView.setText(text);
}
public void bindImage(Bitmap bitmap){
imageView.setImageBitmap(bitmap);
}
public void bindNoteData(String data){
noteData.setText(data);
}
}
XML 文件:activity_group_section.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:orientation="vertical">
<ImageView
android:id="@+id/sectionimage"
android:layout_width="140dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:padding="5dp"
android:src="@drawable/sectionbackground"
/>
<TextView
android:id="@+id/sectionname"
android:layout_width="90dp"
android:layout_height="match_parent"
android:text="@string/textView"
android:visibility="visible"
android:gravity="center"
android:layout_gravity="center_horizontal|top"
android:maxLines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="97dp"
android:layout_height="160dp"
android:id="@+id/noteText"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="10dp"
android:layout_marginTop="30dp" />
</FrameLayout>
</RelativeLayout>
部 fragment 段:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:clipToPadding="false"
android:layout_height="wrap_content" />
Section 和 Notes 的模型类:
public class RestSection {
private int msectionid;
private String msectionname;
private int mxposition;
private int myposition;
private int msectionwidth;
private int msectionheight;
}
public class RestNote {
private int mnoticesid;
private String mnotetext;
private String mnotetag;
private String mnotecolor;
private double mnoteorder;
}
我希望我的问题很清楚,如果有任何需要,请告诉我。
最佳答案
您实际上已经非常接近实现了,但有几件事需要注意。您的顶级网格设置为使用 ListView/GridView 适配器,而您的第二级网格设置为 RecyclerView.Adapters。一旦您知道自己在做什么,这一切都很好。但是,我建议一直使用一个或另一个(最好是 RecyclerView.Adapters,因为它们更具可扩展性)。不过,为简单起见,我将使用您当前的设置给出解决方案:
顶级(BaseAdapter/ListAdapter + GridView)
您应该将整个数据集传递给顶级适配器,例如RestSection 的 ArrayList 而不是 HashMap 的 ArrayList。如果需要此数据,HashMap 可能是 RestSection 的成员,但似乎可以用一个简单的字符串替换它。 RestSection 数据结构还应该有一个成员,它是一个 RestNotes 列表。这个适配器的项目 View 应该有一个 RecyclerView,在你的例子中它在 NoteFragment 里面。这可能会替换 activity_group_section.xml 中的 noteText。在适配器的 getView() 中,您应该使用属于该部分的注释设置此 RecyclerView 的适配器。有关详细信息,请参阅以下代码 fragment 。
数据结构:
public class RestSection {
private int msectionid;
private String msectionname;
private int mxposition;
private int myposition;
private int msectionwidth;
private int msectionheight;
private List<RestNote> mnotes;
}
public class RestNote {
private int mnoticesid;
private String mnotetext;
private String mnotetag;
private String mnotecolor;
private double mnoteorder;
}
SectionLazyAdapter:
public class SectionLazyAdapter extends BaseAdapter{
private List<RestSection> data;
//...
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_group_section, null);
RestSection mySection = data.get(position);
TextView sectionName = (TextView)vi.findViewById(R.id.sectionname); // title
sectionName.setText(mySection.getSectionName());
NoteFragments noteGridView = (NoteFragments) vi.findViewById(R.id.notegridfragment);
noteGridView.setRecyclerViewAdapter(new NoteAdapter(mySection.getNotes()));
return vi;
}
}
activity_group_section.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:orientation="vertical">
<ImageView
android:id="@+id/sectionimage"
android:layout_width="140dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:padding="5dp"
android:src="@drawable/sectionbackground"
/>
<TextView
android:id="@+id/sectionname"
android:layout_width="90dp"
android:layout_height="match_parent"
android:text="@string/textView"
android:visibility="visible"
android:gravity="center"
android:layout_gravity="center_horizontal|top"
android:maxLines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:layout_marginTop="10dp" />
<fragment android:name="com.mypackagename.NoteFragments"
android:layout_width="97dp"
android:layout_height="160dp"
android:id="@+id/notegridfragment"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="10dp"
android:layout_marginTop="30dp" />
</FrameLayout>
</RelativeLayout>
二级(RecyclerView.Adapter + RecyclerView)
这个适配器应该将注释列表作为它的数据集(如上面的 SectionLazyAdapter 所示)。像以前一样使用 Note 数据填充 UI。请参阅下面的代码 fragment 。
笔记 fragment :
public class NoteFragments extends Fragment {
private RecyclerView noteGridView;
// ...
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// as before
noteGridView = (RecyclerView) view;
}
public void setRecyclerViewAdapter(NoteAdapter adapter){
noteGridView.setAdapter(adapter);
}
}
笔记适配器:
public class NoteAdapters extends RecyclerView.Adapter<NoteViewHolder> {
private List<RestNote> mItems;
public NoteAdapters(List<RestNote> notes) {
super();
mItems = notes;
}
@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
// populate as before... make sure that RestNote has all of the data required
}
}
当然,您必须将数据重新排序为所需的格式,但我认为以这种方式显示数据会更容易。
希望对您有所帮助。
关于java - 安卓 : Putting Grid of dynamic&custom objects inside another Grid of dynamic&custom objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31983291/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳